File naming bug when using output mapping
-
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_formatmapping is active (e.g. JPEG → WebP). Crop Thumbnails 1.9.7, WordPress 7.1.21. Crop filenames keep the source extension
With JPEG → WebP mapped, cropping
photo.jpgcreatesphoto.jpg-150x150.webp. WordPress’s own sub-size would bephoto-150x150.webp.Cause: in
CptSaveThumbnail::generateFilename(),$extis switched to the output extension (webp) before the base name is taken.wp_basename($file, '.webp')doesn’t matchphoto.jpg, so.jpgstays in the name. Taking the base name with the original extension before swapping$extfixes 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) andlarge_square(1024×1024 crop) both point tophoto-1024x1024.jpg.Steps:
- Crop
squareonly. - The crop gets a new name,
photo.jpg-1024x1024.webp, sofilter_shouldDeleteOldFile()returns true andphoto-1024x1024.jpgis deleted. large_squarestill points atphoto-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_filenamefilter that strips the stray extension, and acrop_thumbnails_should_delete_old_filefilter at priority 20 that keeps files still referenced by another size. - Crop
You must be logged in to reply to this topic.