Skip to content

Commit

Permalink
Added option to delete only changed images (eg crop, resize, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel W authored and paveljanda committed Jul 22, 2020
1 parent 631c83e commit f445879
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ImageStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,26 @@ public function __construct(

/**
* @param mixed $arg
* @param bool $onlyChangedImages
*/
public function delete($arg): void
public function delete($arg, $onlyChangedImages = false): void
{
$script = is_object($arg) && $arg instanceof Image
? ImageNameScript::fromIdentifier($arg->identifier)
: ImageNameScript::fromName($arg);

$pattern = preg_replace('/__file__/', $script->name, ImageNameScript::PATTERN);
$dir = implode('/', [$this->data_path, $script->namespace, $script->prefix]);
$origFile = $script->name . '.' . $script->extension;

if (!file_exists($dir)) {
return;
}

foreach (new DirectoryIterator($dir) as $file_info) {
if (preg_match($pattern, $file_info->getFilename())) {
if (preg_match($pattern, $file_info->getFilename())
&& (!$onlyChangedImages || ($onlyChangedImages && $origFile !== $file_info->getFilename()))
) {
unlink($file_info->getPathname());
}
}
Expand Down

0 comments on commit f445879

Please sign in to comment.