WordPress.org

Make WordPress Core

Opened 12 months ago

Last modified 2 months ago

#48194 new defect (bug)

Unregistered image sizes used for IMG tag "srcset"

Reported by: galbaras Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords:
Focuses: Cc:

Description

Following design changes, previously-generated thumbnails remain on the site, along with matching post meta entries linked to the respective attachment. WordPress then uses those to produce IMG tags.

In fact, these thumbnails are referenced even when the files don't exist on the site anymore, following a cleanup.

I think WordPress should only use thumbnail URLs for registered image sizes.

For bonus points, there can be a way to remove unregistered size references from attachment meta, perhaps in a Media > Audit admin page, which lets the user decide what to remove and what to keep.

For context, I've just cleaned images on a site and found a lot of legacy references to thumbnails of previously registered sizes in IMG tags. Media management and cleanup plugins I used could only detect the redundant meta data when I re-registered the old sizes temporarily.

Change History (5)

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


9 months ago

#2 follow-up: @joemcgill
9 months ago

Hi @galbaras. Thanks for reporting this issue.

WordPress makes an assumption that if an image size is present in attachment metadata, then the file is available to be served to user requests and can be included in srcset attributes. The best way to clean up unused image sizes from the database is to regenerate thumbnails after changing themes. You can use a plugin like https://wordpress.org/plugins/regenerate-thumbnails/ or the wp media regenerate WP-CLI command.

Does that help?

#3 @galbaras
9 months ago

Hi @joemcgill ,

I use a different plugin to regenerate thumbnails (Image Regenerate & Select Crop), after having problems with the one you've suggested and getting no support. Alas, no WP-CLI (shared hosting).

At my request, the author of IRSC (@iulia-cazan) added a feature to delete orphaned image meta, which I've used since starting this ticket.

As for assumptions, plugins aren't perfect, and people sometimes make changes outside of WordPress, so turns out this assumption is dangerous.

Since WordPress already runs cron jobs, this image meta audit can be very gradual and run monthly or less, but I still think it will be good practice.

Honestly, I've had a lot to do with image handling and optimisation in WordPress lately and this module may need some rethinking. For example, old embeds lack the "wp-image-XXXX" class, and there's nothing in WordPress to bring them up to date, so no srcset :(

I've written a plugin for doing this last bit, and am happy to share my code (works great, but will need some serious cleanup).

But this isn't the only problem. Thumbnail regeneration must be integrated with image optimisation, image file renaming and other functions, and WordPress doesn't facilitate this well enough, it turns out, so the plugin authors have to collaborate directly, rather than work through a good medium.

Cheers,
Gal

#4 in reply to: ↑ 2 @SherylHohman
2 months ago

Replying to joemcgill:

Hi @galbaras. Thanks for reporting this issue.

WordPress makes an assumption that if an image size is present in attachment metadata, then the file is available to be served to user requests and can be included in srcset attributes. The best way to clean up unused image sizes from the database is to regenerate thumbnails after changing themes. You can use a plugin like https://wordpress.org/plugins/regenerate-thumbnails/ or the wp media regenerate WP-CLI command.

Does that help?

No.
In my case I do NOT want to regenerate thumbnails. I want to delete generated thumbnails that I will never use. I have no problem deleting them from the server, but I cannot find any plugin or code that will enable me to delete references to these thumbnail sizes that no longer exist.

There are goo reasons for wanting to selectively delete auto-generated thumbnails.
1) for some images the auto-generated thumbnails are of *very poor quality*. So I manually resize these images and *also* upload them to the server. But when I do so, WP also generates all the same registered sizes for every one of these copies too. So now I have a new original for every size I want, and yet another poorly resized copy for each.
In such cases, I delete the generated sizes of the original full size, for each size that I am uploading a custom resized version. And then I delete all thumbs for the custom resized versions.
But all these thumbnail sizes and copies are still listed in the db. Which is confusing. It makes it difficult to find and select the correct version of an image I want to use. And it means I too easily can choose a url (for an image) that does not exist.
And it is very messy to see registered thumbs for non-existent thumbs.

2) There are many images for which I will NEVER use most of the generated sizes. Many images can only be used for specific uses. Many sizes are not appropriate for all images. For example a shopping cart may generate a size it needs. That is great for products. But for non-products, they will never be needed.
Or specifically designed ultra-wide-very-short images that work for a header, or slider, or for an ad. But can never be used anywhere else - in fact many generated thumbs may crop these to produce unusable pics anyway.

I use a plugin that shows the generated thumb sizes next to every media library entry. From there I can easily grab the correct url for the size image I want. But after deleting bogus thumbs, all sizes that originally generated and registered still show up in the list. Not only is it unnecessarily cluttered, but it shows me that there is a ton on stale info in the database that has zero way of being cleared/cleaned up.

I have tried many plugins, and looked at WP-CLI. There are methods available to delete ALL thumbnails (which could *also* delete non-thumbnails when the user unwittingly happened to name their orig image similar to the way WordPress names its resized copies).
But there is NO way to selectively delete thumbnail sizes for specific images.
Nor is there any way to clean the database if I manually delete thumbs from the server.

There is a way to check for orphaned *original* images, and clean those references from the database.
But that is as close as it gets. And that is to say, useless here.

We should have a way remove dead references to any image that does not exist, whether it is a full size image, or a thumbnail. The former exists. The latter does not.

Here, I very specifically do NOT want to regenerate these images.

Can anyone point me to WordPress code that can enlighten me on how to remove database references to non-existent thumbnail images? This should work whether the thumb size is *currently* a registered size, or if it is *not* currently a registered size.

Or a way to selectively delete thumbs, through WP Media Library, (without forcing regeneration), (or any plugin or command line option) so it *also* cleans the DB references to the deleted images?

#5 @galbaras
2 months ago

@SherylHohman take a look at Image Regenerate & Select Crop. It should do most, if not all, of what you need.

Still, I find the information inconsistency in WordPress problematic, and I don't think there should be a need for a plugin to update WordPress with image file changes. This should be done automatically.

Note: See TracTickets for help on using tickets.