4

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 . "/" . $data['file'];
    }

    return $sizes;
}

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

2
  • side note: there is actually no reason that wordpress will not be able to generate a large image from a smaller one. Browsers do it, so you should be able to do it in PHP as well, might even use better heuristics than browsers can. Commented Feb 15, 2017 at 16:35
  • @MarkKaplun Yea but there's actually no point for me to reduce the quality. Still, possible.
    – Johansson
    Commented Feb 15, 2017 at 16:41

4 Answers 4

2

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 150x150, the same as the thumbnail size. (I used the 150x150 size here instead of your 300x200 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.

7
  • That's all correct birgir, thanks. I updated the question followed by your answer, i hope you have a bit more time to help me further with this issue. Would it be enough to just set $duplicate to false in my custom multi_resize() function?
    – Johansson
    Commented Feb 16, 2017 at 13:02
  • @JackJohansson, so It looks like you're already extending the image class. Just to test, you could try to override the multi_resize() method with the same code from the original method, but set $duplicate to false, and include your current file adjustments in the metadata as well.
    – birgire
    Commented Feb 16, 2017 at 13:37
  • Yes that's what i thought for one moment. But when i do that, i can no longer get that $slug data that i need to build the directory structure.
    – Johansson
    Commented Feb 16, 2017 at 13:39
  • You should be able to get it inside the $metadata array within the original multi_resize() method. Looks like you're using $sizes confusingly, I think it would be more clear if you used $metadata = parent::multi_resize($sizes); etc..Then I think you can better see the relation to the original method @JackJohansson
    – birgire
    Commented Feb 16, 2017 at 13:45
  • To test, try to duplicate and override the original multi_resize() method where you set $duplicate to false and then right before return $metadata; add your foreach ($metadata as $slug => $data) { $metadata[$slug]['file'] = $slug . "/" . $data['file'];} Or try to add it with a filter like wp_generate_attachment_metadata. @JackJohansson
    – birgire
    Commented Feb 16, 2017 at 13:53
2

To solve the problem i create a custom behavior in functions.php restricted to thumbnails not been created yet and just the same size of the image. As @JackJohansson explain, the function work in wp_generate_attachment_metadata filter.

add_filter('wp_generate_attachment_metadata', 'same_size_image');

function same_size_image($meta){
  global $_wp_additional_image_sizes;

  $file = wp_upload_dir();
  $file = trailingslashit($file['basedir']).$meta['file'];
  $info = getimagesize($file);
  $image = wp_get_image_editor($file);

  foreach ($_wp_additional_image_sizes as $label => $op) {
    if (!isset($meta['sizes'][$label])) {
        $width = $op['width'];
        $height = $op['height'];
        $new_file_path = preg_replace('/\.(.+$)/', '-'.$width.'x'.$height.".$1", $file);
        if ($info[0] == $width) $image->save($new_file_path);
    }
  }

  return $meta;
}
-3

Wordpress should automatically create all specified sizes of an image when it's uploaded to the media library. By specified I mean the sizes that are supplied by the parent and/or child themes with:

add_image_size('name', 320, 200);

.. along with any WP defaults. The above is just a simple example of creating an image derivative for WP. If you want it to create a thumbnail of a specified size every time, you can call this in functions.php (or an include file your theme is calling).

Are you looking for a specific size or just the 100x100 default?

4
  • Thank you but my problem isn't this, the problem is when the uploaded image is 100x100px , the thumbnail isn't generated (since the uploaded image is considered a thumbnail itself).
    – Johansson
    Commented Feb 16, 2017 at 8:04
  • wordpress API do not auto generate images that are bigger than the uploaded image, you can not use it, and need to work around the limitation Commented Feb 16, 2017 at 9:22
  • @MarkKaplun My apologies mark, but i don't see how that's relevant to the question. I never asked to create an image larger than original.
    – Johansson
    Commented Feb 16, 2017 at 12:55
  • @JackJohansson, wrong choice of words, wordpress generates only smaller images. The actual answer will most likely be applicable to both cases (equal and bigger) Commented Feb 16, 2017 at 14:12
-3

Hello i am not sure this is what you are looking for but i think this would help

add_image_size( 'thumbnail', '100px', '100px', true );

use either true or FALSE statement to declear wheather or not to resize image, the above code would produce an image of almost exactly 100px x 100px . for all images how ever small walk arounds needed to be don eon the css.

3
  • Thank you but my problem isn't this, the problem is when the uploaded image is 100x100px , the thumbnail isn't generated (since the uploaded image is considered a thumbnail itself).
    – Johansson
    Commented Feb 16, 2017 at 8:04
  • wow, never saw that coming, why the down vote ? Commented Feb 18, 2017 at 14:09
  • I didn't downvote by the way. Someone downvoted all answers.
    – Johansson
    Commented Feb 18, 2017 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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