9

Is there any filter available to set the naming convention of those auto generated thumbnails?

Something like this:

  • thumbnail_150x150.jpg -> thumbnail-s.jpg
  • thumbnail_300x300.jpg -> thumbnail-m.jpg
  • thumbnail_600x600.jpg -> thumbnail-l.jpg
4

Seems that the answer is no...

I've followed the core functions and found a dead-end. And found this post ( How can I make add_image_size() crop from the top? ) where Rarst says:

Intermediate image generation is extremely rigid. Image_resize() keeps it close to code and completely lacks hooks.

But, following the lead of the other answer (from bradt) and the code he published ( Image Crop Position in WordPress ), I think I got it :)

In the function bt_generate_attachment_metadata, I've just modified the call to bt_image_make_intermediate_size including the last parameter $size

$resized = bt_image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'], $size );

And modified the beggining of the function bt_image_make_intermediate_size as follows:

  • added the $size parameter to the function
  • instead of the default null value to $suffix, a switch to our new suffixes
function bt_image_make_intermediate_size( $file, $width, $height, $crop = false, $size ) {
    if ( $width || $height ) {
        switch($size) {
            case 'thumbnail':
                $suffix = 't';
                break;
            case 'medium':
                $suffix = 'm';
                break;
            case 'large':
                $suffix = 'l';
                break;
            default:
                $suffix = null;
                break;
        }
        $resized_file = bt_image_resize( $file, $width, $height, $crop, $suffix, null, 90 );

Here, a copy of the full code with my mods, just for reference.
And the diff from the original.

Much probably, all the code can be shrank to a bare bone, but this way we get the benefit of custom cropped images :))

| improve this answer | |
  • 1
    sorry, still traveling. I'll test it soon and let you know. Thanks. – Sean Lee May 17 '12 at 11:15
  • Is this answer still valid? It's been a few years, so... – Athoxx Nov 21 '16 at 22:33
2

You could use the filter image_make_intermediate_size, but you would have to figure out what name you want to change the intermediate file to, according to the $filename you get (and then rename the file, because at this point it already has been generated).

I only generate an intermediate size image for the "thumbnail", so it is as simple as this:

add_filter( 'image_make_intermediate_size', function( $filename ) {

    // old 2017_234783843-100x100.jpg
    $old = $filename;
    // new 2017_234783843-thumbnail.jpg
    $new = preg_replace("/(\d+_\d+)-\d+x\d+\.(.*)/i", "$1-thumbnail.$2", $old );

    rename($old, $new);

    return $new;
} );
| improve this answer | |
  • 1
    regex could be less specific "/(.*)-\d+x\d+\.(.*)/i" – benoît Sep 6 '18 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.