vollyimnetz/crop-thumbnails

Old thumbnails not being removed when file name is changed

Closed this issue · 2 comments

To bust CDN caches, I generate a new file name after each thumbnail cropping using the crop_thumbnails_filename filter:

add_filter('crop_thumbnails_filename', function (string $fileName, $file, $width, $height, bool $crop): string {
    if (! $crop) {
        return $fileName;
    }

    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
    $date = new DateTimeImmutable();

    return sprintf(
        '%s-%s.%s',
        rtrim($fileName, '.' . $extension),
        $date->getTimestamp(),
        $extension
    );
}, -10, 5);

However, when I repeatedly crop the thumbnails, the latest thumbnails work but old thumbnails are not being removed from the server.

Looks like

self::addDebug('Image filename has changed ('.$activeImageSize->name . ')');
$changedImageName[ $activeImageSize->name ] = true;
never got triggered.

Am I using crop_thumbnails_filename incorrectly?


Versions

WordPress v5.5.1
Crop-Thumbnails v1.2.6

Hi,
i would recommend to use the "crop_thumbnails_after_save_new_thumb" action and the "crop_thumbnails_before_update_metadata" filter for your code.

At this point the image is already saved. Now the only thing to do, is to rename the file on the server and then change the metadata.

  1. rename the image file in "crop_thumbnails_after_save_new_thumb" and store nessesary data in a global variable.
  2. in "crop_thumbnails_before_update_metadata" use the data from the global variable to store the correct file-path in the metadata.

Changes are added in version 1.3