Last active
April 22, 2020 13:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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