Skip to content

Instantly share code, notes, and snippets.

@rustam-swe
Created May 3, 2021 09:56
Show Gist options
  • Save rustam-swe/250e6c256b8b0f26d065ab7f01131511 to your computer and use it in GitHub Desktop.
Save rustam-swe/250e6c256b8b0f26d065ab7f01131511 to your computer and use it in GitHub Desktop.
Returns dimensions (width and height) of a given svg (by url)
/**
* Returns dimensions (width and height) of a given svg (by url)
*
* @param string $image_url
* @source https://stackoverflow.com/a/6532357/9214537
*
* @return string
*/
function get_svg_dimensions( string $image_url ): string {
$parsed_xml = simplexml_load_string( file_get_contents( $image_url ) );
$svg_attributes = $parsed_xml->attributes();
$width = (string) $svg_attributes->width;
$height = (string) $svg_attributes->height;
return "width=\"{$width}\" height=\"{$height}\"";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment