[wp-trac] [WordPress Trac] #18947: get_intermediate_image_sizes() should also contain width/height/crop values as sub array

WordPress Trac noreply at wordpress.org
Thu Jun 5 06:26:26 UTC 2014


#18947: get_intermediate_image_sizes() should also contain width/height/crop values
as sub array
-----------------------------------+-----------------------
 Reporter:  F J Kaiser             |       Owner:
     Type:  enhancement            |      Status:  assigned
 Priority:  normal                 |   Milestone:  4.0
Component:  Media                  |     Version:  3.2.1
 Severity:  normal                 |  Resolution:
 Keywords:  has-patch 2nd-opinion  |     Focuses:
-----------------------------------+-----------------------

Comment (by krogsgard):

 As shown in 18947_5_2.patch, `get_image_sizes_data()` won't return
 anything for custom image sizes, since that data isn't in the database and
 the global that stores that data isn't in the `foreach` loop, though the
 global is getting called from `get_intermediate_image_sizes()` anyway.

 Perhaps another route is to create `get_image_size_data()` instead, which
 would also have more uniformity with `add_image_size()`. Then
 (unfortunately), a second function for `get_image_sizes_data()` that calls
 the first in its own `foreach` loop.

 If you do something like the following, you can get single image size data
 without calling the global, as long as it's a standard size. It also
 avoids calling `get_intermediate_image_sizes()`. That function is a
 nightmare of a name considering it gets **all** image sizes, but says it
 only gets intermediate.

 {{{
 function get_image_size_data( $size = 'thumbnail' ) {
         $default_image_sizes = array( 'thumbnail', 'medium', 'large' ); //
 Standard sizes
         if ( in_array( $size, $default_image_sizes ) ) {
                 $result['width'] = intval( get_option( "{$size}_size_w" )
 );
                 $result['height'] = intval( get_option( "{$size}_size_h" )
 );
                 // If not set: crop false per default.
                 $result[ $size ]['crop']   = false;
                 if ( get_option( "{$size}_crop" ) ) {
                         $result[ $size ]['crop'] = get_option(
 "{$size}_crop" );
                 }
         } else {
                 global $_wp_additional_image_sizes;
                 if ( in_array( $size, array_keys(
 $_wp_additional_image_sizes ) ) ) {
                         $result = $_wp_additional_image_sizes[ $size ];
                 }
         }

         return $result;
 }
 }}}

 I think the ability to just get a single image size's data may suffice for
 many use cases (like a function to call to a CDN or something). When it's
 not, the second function to loop through all image sizes could call this
 one.

 I'm not certain two functions is the best route, but I certainly don't
 think `get_image_sizes_data()` serves much purpose without the ability to
 get custom image size data in addition to defaults.

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


More information about the wp-trac mailing list