1

As we know, image's sizes have different names which we can use to call the appropriate image size. Like 'thumbnail', 'large', 'medium' or a custom 'my-thumbnail' size. I can the_post_thumbnail_url( 'medium' ) to get medium size and it's dimensions.

however, i need to get the NAME from the image size. To be precise, if the image size is 300x200 and it's name is 'small', i want to get 'small' by entering 300 and 200 into a function.

Can anyone help me achieve this please? Thanks.

4
  • It's possible, depending on the image sizes that several themes or plugins may create, for an image file to match more than one image size. If you have medium set to 300x300 and medium-crop set to 300x200 with hard crop then an uploaded image of 600x400 will use the same 300x200 image for both of these sizes. What result would you prefer from your function in that case? Commented Oct 21, 2016 at 8:52
  • I just have thumbnail, medium and another custom 'my-thumbnail' size. i would like to get image's dimension and check whether if it's one of these sizes or not.
    – Johansson
    Commented Oct 21, 2016 at 15:23
  • And are you starting with the filename? Commented Oct 21, 2016 at 15:25
  • no i just have 2 integers (width & heigh) and want the size name. thanks for the time, the code provided by @the_dramatist is exactly what i wanted.
    – Johansson
    Commented Oct 21, 2016 at 15:29

1 Answer 1

1

Here I've written a function for you. Place this below function on your functions.php or any where that executes and pass the width and height value to it. It will return you the image size name as string and if it has not found any then it will return false.

/**
 * Get name by size information for image.
 *
 * @global $_wp_additional_image_sizes
 * @uses   get_intermediate_image_sizes()
 * @return string $_sizes bool Data for passed measurements image.
 */
function wpse_243461_get_image_size_name( $w = 150, $h = 150 ) {
    global $_wp_additional_image_sizes;

    $sizes = array();
    foreach ( get_intermediate_image_sizes() as $_size ) {
        if ( in_array( $_size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) {
            if ( $w == get_option( "{$_size}_size_w" ) && $h == get_option( "{$_size}_size_h" ))
                return $_size;
        } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
            if ( $w == $_wp_additional_image_sizes[ $_size ]['width'] && $h == $_wp_additional_image_sizes[ $_size ]['height'] )
                return $_size;
        }
    }

    return false;
}

By default it is taking width 150 and height 150 which is passed like this $w = 150, $h = 150. And you have to use it like the_dramatist_get_image_size_name(300, 300). Please keep it in mind that $w and $h takes integer value.

6
  • What if your medium size is set to 300x300 without hard crop, so the image being inspected might be 300x200 or 200x300? Commented Oct 21, 2016 at 8:40
  • As far I know, there is no rules of how you name your functions. It's a good practice to prefix your functions, but you can use whatever prefix you want. Use the namescpace wpse_xxxx if you want; don't use it if you don't want to. See meta.wordpress.stackexchange.com/questions/1330/…
    – cybmeta
    Commented Oct 21, 2016 at 8:41
  • It's checking the both dimensions of a single image. See here, if ( $w == get_option( "{$_size}_size_w" ) && $h == get_option( "{$_size}_size_h" )) and here if ( $w == $_wp_additional_image_sizes[ $_size ]['width'] && $h == $_wp_additional_image_sizes[ $_size ]['height'] ). I don't think there is a problem. @AndyMacaulay-Brook
    – CodeMascot
    Commented Oct 21, 2016 at 8:44
  • @cybmeta I couldn't see a reference but got told off by someone with a lot of rep for not doing it :-S Commented Oct 21, 2016 at 8:45
  • So it only returns true if both the width and the height match exactly. Commented Oct 21, 2016 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.