[wp-trac] [WordPress Trac] #40370: add_image_sizes does not create the "crop position" versions of the image

WordPress Trac noreply at wordpress.org
Wed Oct 10 03:20:07 UTC 2018


#40370: add_image_sizes does not create the "crop position" versions of the image
--------------------------+------------------------------
 Reporter:  piejesus      |       Owner:  (none)
     Type:  enhancement   |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Media         |     Version:  2.9
 Severity:  normal        |  Resolution:
 Keywords:  dev-feedback  |     Focuses:
--------------------------+------------------------------

Old description:

> I wanted to introduce 3 different version of post thumbnails - each with
> different cropping position (top, center, left) , so i added them like
> this:
>
> {{{#!php
> 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:
>
> {{{#!php
> 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

New description:

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

 {{{#!php
 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:

 {{{#!php
 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

--

Comment (by peterwilsoncc):

 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
 <?php

 $dims = [
   0 => 0
   1 => 0
   3 => // Crop start X axis
   4 => // Crop start Y axis
   5 => // New width
   6 => // New height
   7 => // Crop width on source image
   8 => // Crop height on source image
 ];
 }}}

 The crop hash could be generated from
 `$source_w,$source_h,$dims[3],$dims[4],$dims[7],$dims[8]`. This will
 ensure images from the same source with the same aspect ratio and crop
 will end up with the same hash.

 If the final two values are the same as the source image then no hash is
 required.

 When generating the `srcset` in `wp_calculate_image_srcset()` the crop
 hash can be checked in the same way as that the edit suffix is checked.

 In terms of backward compatibility I don't think there are implications as
 previously uploaded images will not have a crop hash.

 cc @joemcgill as we've discussed this previously.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/40370#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list