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

WordPress Trac noreply at wordpress.org
Sat Aug 24 04:43:03 UTC 2019


#40370: add_image_sizes does not create the "crop position" versions of the image
-------------------------------------------------+-------------------------
 Reporter:  piejesus                             |       Owner:  (none)
     Type:  enhancement                          |      Status:  reopened
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Media                                |     Version:  2.9
 Severity:  normal                               |  Resolution:
 Keywords:  needs-unit-tests good-first-bug      |     Focuses:
  has-patch 2nd-opinion                          |
-------------------------------------------------+-------------------------

Comment (by azaozz):

 There was a discussion in the
 [https://wordpress.slack.com/archives/C02SX62S6/p1566609255394300?thread_ts=1566481102.355100&cid=C02SX62S6
 #core-media channel on Slack] about image sub-size names and file names.

 There are several related tickets and couple of ideas about how to fix
 them. Generally the solution seems to be to add a short hash to image sub-
 size file names (like in 40370.diff) but in order to fix the file name
 clashing with `-WxH`, it would need to add that hash in all cases, not
 just for cropped images. Then the hash can be generated from the sub-size
 settings array. For example:
 {{{
 $size_settings = array (
     'width'  => 1024,
     'height' => 1024,
     'crop'   => false,
 ),
 }}}

 would result in:
 {{{
 $str = '';

 // `crop` may be an array.
 array_walk_recursive( $size_settings, function( $value ) {
      $str .= $value;
 } );

 $hash = substr( md5( $str ), 0, 8 );
 }}}

 Another option would be to add the "size name" to the image file name.
 That should work properly as the size name is unique, but may make the
 file name too long in some cases (may need to shorten it). Example:
 {{{
 [my-image-name]-medium-400x300.jpg
 [my-image-name]-large-1024x768.jpg
 [my-image-name]-newscentered-400x400.jpg
 [my-image-name]-newstop-400x400.jpg
 ...
 }}}

 Yet another idea is to append `-1`, `-2`, etc. to the file name (before
 the `-WxH` part) when testing for unique file name. That will be a bit
 more complex in case of sizes with the same dimensions and different crop
 values. It will have to keep previously tested and approved file names in
 cache and test/increment against them too.

 Related: #42437.

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


More information about the wp-trac mailing list