Skip to content

Instantly share code, notes, and snippets.

@deviationist
Last active April 22, 2020 13:15
Show Gist options
  • Save deviationist/aec189bbdd88a62856c989a31f997127 to your computer and use it in GitHub Desktop.
Save deviationist/aec189bbdd88a62856c989a31f997127 to your computer and use it in GitHub Desktop.
Extract the dimension from an uploaded image URL in WordPress. Only works for images with the image size in the filename.
/**
* Extract the width and height from the URL of a resized image file in Wordpress.
*
* @param $url
*
* @return array|bool
*/
function exctract_wordpress_image_size_from_url($url) {
preg_match('/-(?P<width>[0-9]+)x(?P<height>[0-9]+).(.+)$/', $url, $matches);
if ( array_key_exists('width', $matches) && array_key_exists('width', $matches) ) {
$width = $matches['width'];
$height = $matches['height'];
return compact('width', 'height');
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment