WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 6 months ago

#12120 new feature request

Thumbnail image isn't created if the original is too small

Reported by: atow Owned by:
Milestone: Awaiting Review Priority: low
Severity: normal Version:
Component: Media Keywords: has-patch needs-refresh
Focuses: Cc:

Description (last modified by SergeyBiryukov)

It would be very useful to force the generation of a thumbnail images for uploaded files. With content being syndicated to feeds, e-readers, and mobile devices, the ability to have a working thumbnail is more important.

For example, if the original image has dimensions of 250x50, and the default thumbnail size in WP is 150x150, a thumbnail will not be generated because the 50 height is smaller than the 150 pixel minimum.

The fix would be to include an additional parameter to the image_resize_dimensions to allow the function the dimensions. A filter could be applied to allow the user to set which images should force the generation of a resized image (i.e. to exclude a medium and large image from being generated for a 250x50 image) by passing in the original width/height and max width/height.

in wp-includes/media.php

========

function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {

...

$force = apply_filters('image_resize_dimensions_force', $orig_w, $orig_h, $max_w, $max_h);
$dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $force);

========

function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false, $force = false) {
...

// if the resulting image would be the same size or larger we don't want to resize it
	if ( $new_w >= $orig_w && $new_h >= $orig_h && !$force )
		return false;

Attachments (2)

force_image_resize.diff (5.2 KB) - added by Kanuck54 5 years ago.
12120.diff (10.7 KB) - added by ericlewis 8 months ago.

Download all attachments as: .zip

Change History (17)

comment:1 @Kanuck545 years ago

  • Keywords has-patch needs-testing added; media thumbnail removed
  • Version 2.9.1 deleted

I wrote a patch to do this. It adds a force_image_resize filter, which receives the name of the image size ('thumbnail', 'medium', etc.) so that they can be selectively forced to resize.

comment:2 @Kanuck545 years ago

Ah yes, I should also mention my reason for doing this: PHP handles CMYK JPEG files just fine, whereas Internet Explorer does not. My client kept uploading CMYK JPEGs, not knowing the difference between that and an RGB JPEG.

The bad solution was to tell him to stop uploading CMYK JPEGs. The good solution was to write this patch, set force_image_resize to true, use the "large" thumbnail, and tell him "problem solved!" PHP generates an RGB JPEG from his CMYK JPEG, Internet Explorer displays it successfully, and all is right in the world.

comment:3 @landwire18 months ago

Came across this ticket, as I need some images always to create a thumbnail/image size. Has this been incorporated?

comment:4 @SergeyBiryukov18 months ago

  • Description modified (diff)

Related: #15311, #21295.

comment:5 @landwire18 months ago

Ah, thanks. The related tickets help and are more up to date.

comment:6 @ericlewis8 months ago

I think we should just scale up images when cutting a crop and the original is smaller than the size, and avoid this issue completely.

Last edited 8 months ago by ericlewis (previous) (diff)

comment:7 @ericlewis8 months ago

  • Milestone changed from Future Release to 4.2

@ericlewis8 months ago

comment:8 @ericlewis8 months ago

The fact that a thumb size in cases like @atow described in the original report is a bummer. Instead of not generating image sizes bigger than the original, we should scale up so that these cuts are generated. Whether they are pixelated shouldn't be WordPress' business, which I'm guessing was the idea behind not creating the bigger cuts.

image_make_intermediate_size() is the public function for resizing an image. Although there are also public methods on the WP_Image_Editor_* instance, image_make_intermediate_size() is a façade layer for this.

image_resize_dimensions() seems to be the place where logic to do regarding resizing, as it generates the parameters for imagecopyresampled(), so I've modified some of this logic.

attachment:12120.diff is a go at this.

image_resize_dimensions() has been modified to default to upscaling images.

I've also created a new function, wp_scale_dimensions(), which is wp_constrain_dimensions() but with an option for upscaling. wp_constrain_dimensions() now calls wp_scale_dimensions() with a disallow upscaling flag for backwards compatibility. We could've jammed this logic into wp_constrain_dimensions(), but it seems apt to make a separate, more self-describing function with the extra option.

I added a unit test and modified old ones which expected images not to be scaled up.

comment:9 @ericlewis8 months ago

  • Summary changed from Force Media Image Generation (for thumbnails) to Thumbnail image isn't created if the original is too small

comment:10 @DrewAPicture7 months ago

  • Priority changed from normal to low
  • Type changed from enhancement to feature request

comment:11 @alordiel7 months ago

  • Keywords needs-testing removed

@ericlewis's patch tested on WordPress 4.1 and WordPress 3.9

uploading image with original size 250x50

Without the patch: two files are generated the original one and orginal-150x50

Results after applying the patch:
4 files are generated:
original
original-150x50
original-300x60
original-1024x205

comment:12 @SergeyBiryukov7 months ago

#13014 was marked as a duplicate.

comment:14 @wonderboymusic6 months ago

  • Keywords needs-refresh added

The patch is exploding

comment:15 @helen6 months ago

  • Milestone changed from 4.2 to Awaiting Review

Needs a refresh, and also I really hate the idea of scaling images up in a file by default. I'd rather point the thumbnail size to the full size or something. If I'm uploading a small icon, the last thing I want is upscaled, likely significantly larger files (antialiasing, etc.) that I'll never use.

Note: See TracTickets for help on using tickets.