I’ve noticed that WordPress does not generate a thumbnail for an image if the uploaded image’s size is the same as the thumbnail’s size.

To make this clear, here is an example:

I have an image with a size of 300x200px. My thumbnail’s size in the WordPress setting is also 300x200. So, when i upload this image, no thumbnail of 300x200 size will be generated, because the uploaded image itself is considered a thumbnail to WordPress!

I’ve tracked it down to wp_generate_attachment_metadata() function, which decides not to crop an image if the image’s size is equal to a thumbnail’s size. Here is the link to this function’s content.

This function has a filter, as the following:

apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );

How can i make this function to make the thumbnail, no matter what size?

Thanks.

UPDATE 1:

Following the answer by @birgire, i have included my custom multi_resize() function in the question:

function multi_resize($sizes) {
    $sizes = parent::multi_resize($sizes);

    //we add the slug to the file path
    foreach ($sizes as $slug => $data) {
        $sizes[$slug]['file'] = $slug . "https://wordpress.stackexchange.com/" . $data['file'];
    }

    return $sizes;
}

I’m using this function for some other purposes, mentioned this question.

4 Answers
4

If I understand the question correctly, you want to generate

test.jpg
test-150x150.jpg

instead of just:

test.jpg

when you upload an image called test.jpg, of size 150×150, the same as the thumbnail size. (I used the 150×150 size here instead of your 300×200 to avoid confusing it with the medium size)

This restriction is implemented in the multi_resize() method of the image editor classes, like WP_Image_Editor_Imagick and WP_Image_Editor_GD that extend WP_Image_Editor:

$duplicate = ( ( $orig_size['width'] == $size_data['width'] ) 
    && ( $orig_size['height'] == $size_data['height'] ) );

If the size is the same as the size of the original image, then it’s not generated.

A possible workaround could be to extend the image class to your needs and add it to the list of available image editors through the wp_image_editors filter.

Alternatively you could try to hook into the wp_generate_attachment_metadata to generate the missing duplicate with a help from the image editor, like the save() method.

Leave a Reply

Your email address will not be published. Required fields are marked *