Site icon Hip-Hop Website Design and Development

Getting the closest named image size from an array of dimensions

I want to retrieve the closest named image size (e.g. 'thumbnail', 'medium', 'large', 'full', or the name of any custom sizes added with add_image_size()) from an array of width/height values (e.g. array( 150, 150 )). I essentially want to do what wp_get_attachment_image and related functions do but without an actual image.

What I’m hoping for is something like:

$image_size   = get_named_size( array( 150, 150 ) ); // returns 'thumbnail'
$another_size = get_named_size( array( 999999, 999999 ) ); // returns 'full'

It looks like all the resizing magic in the image functions happens in image_downsize(), but as far as I can see everything that happens there relies on an actual image and only returns a new image, not a size, which doesn’t really help me.

I can get all the existing sizes using something like the examples here, but comparing my array values with all the existing sizes and finding the closest is going to be a bit cumbersome so I was hoping there was an existing wp function that could help me out.