• Context: we’re using a filter to set the image editor output format to webp. Originals are uploaded as jpeg or png and resized versions are all generated as webp.

    add_filter(‘image_editor_output_format’, function (array $formats): array {
    $formats[‘image/jpeg’] = ‘image/webp’;
    $formats[‘image/png’] = ‘image/webp’;
    return $formats;
    });

    Two bugs when an image_editor_output_format mapping is active (e.g. JPEG → WebP). Crop Thumbnails 1.9.7, WordPress 7.1.2

    1. Crop filenames keep the source extension

    With JPEG → WebP mapped, cropping photo.jpg creates photo.jpg-150x150.webp. WordPress’s own sub-size would be photo-150x150.webp.

    Cause: in CptSaveThumbnail::generateFilename(), $ext is switched to the output extension (webp) before the base name is taken. wp_basename($file, '.webp') doesn’t match photo.jpg, so .jpg stays in the name. Taking the base name with the original extension before swapping $ext fixes it.

    2. Re-cropping a size can delete a file another size still uses

    WordPress gives sizes with identical dimensions one shared file. For example, square (1024×1024 crop) and large_square (1024×1024 crop) both point to photo-1024x1024.jpg.

    Steps:

    1. Crop square only.
    2. The crop gets a new name, photo.jpg-1024x1024.webp, so filter_shouldDeleteOldFile() returns true and photo-1024x1024.jpg is deleted.
    3. large_square still points at photo-1024x1024.jpg, which no longer exists, so it renders as a broken image.

    This happens whenever the crop’s filename differs from the old one, which is every crop on a site using an output-format mapping. Suggested fix: before deleting, check that no other size in the attachment metadata references the same file.

    Workarounds we’re using in the meantime: a crop_thumbnails_filename filter that strips the stray extension, and a crop_thumbnails_should_delete_old_file filter at priority 20 that keeps files still referenced by another size.

You must be logged in to reply to this topic.