• It seems as if the mechanics for generating image sizes for responsive design (srcset, thumbnails etc.) can only use a single set of image sizes across an entire blog/site (I know how to extend that set globally via functions.php and have already done so).

    Is there a way to specify additional sizes for a single image in the library?

    For example if a single image used in a single post has an unusual natural width, it is easy to specify that it should be shown in such a that size via the editor, but how would one specify that the additional sizes generated for the srcset should correspond to typical multiples of that such as 1.5x and 2x, instead of/in addition to the sizes that are generated for all other images on the blog.

    It kind of sucks, in terms of disk space and srcset html bloat for all the other images, to add the one off sizes to the global list in theme/themename/functions.php. But it also sucks to have web site testing tools complain about client-side shrinking to the one-off size.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You can filter the the srcset list and sizes attribute, as well as the image tag that is output, but by default WP only generates smaller images as the are defined by the registered sizes. So while you can add the information, there are likely few, if any, images of the correct aspect that are not cropped that you could use in your list. So the problem is generating smaller images.

    I get that you know that, I’m just explaining for anyone else who lands here. The sizes of smaller images generated on upload can be filtered with “intermediate_image_sizes_advanced”. The problem appears to be that you need to know what sizes you want before the image is uploaded. But you don’t know the image size until it’s uploaded! But all is not lost. There is one filter in the upload process that fires before the advanced image sizes: “wp_handle_upload”. Here you can access the file’s URL and filename and file type. This filter fires for any media upload, not just images.

    With the filename, you can use getimagesize() to determine the original image size and calculate what images you would like generated. Add a filter hook to “intermediate_image_sizes_advanced” that adds these sizes to the list. You can also remove any undesirable sizes that WP would generate by default. Once your callback has added sizes, it can remove itself so that it does not influence any other uploads.

    This is untested, but I don’t see why it wouldn’t work. Your custom sizes ought to be saved in attachment post meta like registered sizes are, so I believe they would end up in srcset like all the others by default, but I have not verified this. Of course if it doesn’t happen, srcset is filterable, so that shortcoming can be addressed if need be.

    The one other hurdle is there is no simple way to pass the sizes you determined in “wp_handle_upload” to your callback to “intermediate_image_sizes_advanced”. The easiest is to utilize global variables. Many coders find globals abhorrent, not without reason. If they bother you, you can manage the values and callbacks in a class object.

Viewing 1 replies (of 1 total)
  • The topic ‘One-off image sizes?’ is closed to new replies.