💥 TRENDING: Ticket/ - Collection

Opened 9 years ago

Last modified 20 months ago

#40370 reopened enhancement

add_image_sizes does not create the "crop position" versions of the image

Reported by: piejesus's profile piejesus Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 2.9
Component: Media Keywords: good-first-bug has-patch 2nd-opinion has-unit-tests needs-testing
Focuses: Cc:

Description (last modified by joemcgill)

I wanted to introduce 3 different version of post thumbnails - each with different cropping position (top, center, left) , so i added them like this:

add_image_size( 'newscentered', 400, 400, array( 'center', 'center') );
add_image_size( 'newstop', 400, 400, array( 'center', 'top' ) );
add_image_size( 'newsbottom', 400, 400, array( 'center', 'bottom' ) );

Now, whenever i use the the_post_thumbnail() with the name of my custom image size i always get the same image, cropped to the default WordPress crop position of ('center', 'center') .

Why is that happening? I did 'refresh' the thumbnails and tried also uploading fresh image files and still i can't get 3 differently cropped versions of the image.

I noticed that when i set the cropping defaults using the following function and set the cropping there, then it works:

set_post_thumbnail_size( 400, 400, array('center', 'bottom'));

... but it affects the cropping of all of my thumbnails, so i can only get one "crop position" for all my images.

Guys, is this some kind of bug or do i configure something in a wrong way?

I'm using the newest official WordPress version

Attachments (5)

40370.diff (4.7 KB) - added by themattroyal 7 years ago.
40370.patch (1.9 KB) - added by nickylimjj 7 years ago.
Unit test with FIXME
Fix incompatible declarations PHP Warnings - patch.diff (1.9 KB) - added by davidbawiec 5 years ago.
Fix PHP Warnings relating to incompatible declarations with WP_Image_Editor::update_crop_hash($hash = NULL).
40370-refresh-at-6-4.patch (7.2 KB) - added by misfist 2 years ago.
Upload patch previously submitted https://github.com/WordPress/wordpress-develop/pull/5086
40370-refresh-6-4-descriptions.patch (7.8 KB) - added by misfist 2 years ago.
Successfully Patched against 6.4 - https://github.com/misfist/wordpress-develop/pull/3

Download all attachments as: .zip

Change History (41)

#1 follow-up: @sebastian.pisula
9 years ago

  • Keywords dev-feedback added

I think that this is bug in this because each file will be name for example "filename-400x400.jpg" but should be "filename-400x400-center-center.jpg", "filename-400x400-center-top.jpg", "filename-400x400-center-bottom.jpg"

Last registered size will be overwrite file

#2 in reply to: ↑ 1 @piejesus
9 years ago

Ay, yes, this is exactly what's happening, just checked it by changing the sizes by 1 pixel. Thanks you!

Replying to sebastian.pisula:

I think that this is bug in this because each file will be name for example "filename-400x400.jpg" but should be "filename-400x400-center-center.jpg", "filename-400x400-center-top.jpg", "filename-400x400-center-bottom.jpg"

Last registered size will be overwrite file

#3 @swissspidy
9 years ago

  • Focuses administration removed
  • Type changed from defect (bug) to enhancement
  • Version changed from 4.7.3 to 2.9

Introduced in [12342] and extended in #27472.

As @sebastian.pisula mentioned, WordPress stores the files with the dimensions only, nothing in addition to that. I wouldn't consider this a bug though, but expected behaviour.

#4 @alexvorn2
9 years ago

I can advice a plugin, maybe it will help - https://wordpress.org/plugins/resize-post-thumbnails/

#5 in reply to: ↑ description @anonymized_5746546
8 years ago

Replying to piejesus:

I wanted to introduce 3 different version of post thumbnails - each with different cropping position (top, center, left) , so i added them like this:

add_image_size( 'newscentered', 400, 400, array( 'center', 'center') );
add_image_size( 'newstop', 400, 400, array( 'center', 'top' ) );
add_image_size( 'newsbottom', 400, 400, array( 'center', 'bottom' ) );

Now, whenever i use the the_post_thumbnail() with the name of my custom image size i always get the same image, cropped to the default Wordpress crop position of ('center', 'center') .

Why is that happening? I did 'refresh' the thumbnails and tried also uploading fresh image files and still i can't get 3 differently cropped versions of the image.

I noticed that when i set the cropping defaults using the following function and set the cropping there, then it works:

set_post_thumbnail_size( 400, 400, array('center', 'bottom'));

... but it affects the cropping of all of my thumbnails, so i can only get one "crop position" for all my images.

Guys, is this some kind of bug or do i configure something in a wrong way?

I'm using the newest official Wordpress version

Hello, I'm using version 4.9 now (2017) and the same thing is still happening. Was wondering if it had been "enhanced" in previous versions?

#6 @peterwilsoncc
7 years ago

  • Description modified (diff)

I've hit this recently too, the problem occurs during the upload process.

Using the crop names in the original report, the three images result in the same file name. During resize newscentered is saved, it's then replaced by the newstop crop which is in turn replaced by the newsbottom crop.

While site owners don't get the crops they expect, a side-effect is the srcset for an image may contain different crop orientations.

I suggest files be saved with an eight character hash of the crop information returned by image_resize_dimensions() to ensure neither of these problems occur. The file name could be uploaded-name-c{$hash}.ext.

image_resize_dimensions()'s return array is:

<?php

$dims = [
  0 => 0 
  1 => 0 
  2 => // Crop start X axis
  3 => // Crop start Y axis
  4 => // New width
  5 => // New height
  6