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.
medium
set to 300x300 andmedium-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?