function svg_image_get_dimensions in Svg Image 7
Get an SVG image's defined dimensions.
Parameters
string $uri: The URI or path to an SVG image.
Return value
array|FALSE An array containing the keys "width" and "height" as integer values. If the image is an SVG but no set dimensions are available, these keys will have NULL values. If the image is not an SVG, FALSE will be return.
3 calls to svg_image_get_dimensions()
- svg_image_field_attach_presave in ./
svg_image.module - Implements hook_field_attach_presave().
- svg_image_file_presave in ./
svg_image.module - Implements hook_file_presave().
- svg_image_preprocess_image_style in ./
svg_image.module - Preprocess function for theme_image_style().
File
- ./
svg_image.module, line 127
Code
function svg_image_get_dimensions($uri) {
// Safety check.
if (!svg_image_is_svg($uri)) {
return FALSE;
}
// Return cached dimensions if already retrieved once this request.
$cached_images =& drupal_static(__FUNCTION__, array());
if (isset($cached_images[$uri])) {
return $cached_images[$uri];
}
// Workaround for a getting a width and height. It cannot be null and need to
// be defined for the every image. So - if we got SVG file without the
// dimensions defined in attributes it should be defined from our side to
// avoid an exception.
// @todo add a variable which defines this values.
$svg_dimensions = array(
'width' => SVG_IMAGE_DEFAULT_WIDTH,
'height' => SVG_IMAGE_DEFAULT_HEIGHT,
);
$file_contents = file_get_contents($uri);
if ($file_contents) {
$svg = simplexml_load_string($file_contents);
foreach ($svg
->attributes() as $attribute => $value) {
if ($attribute === 'width' || $attribute === 'height') {
$svg_dimensions[$attribute] = (int) $value;
}
}
}
// Save values to static cache.
$cached_images[$uri] = $svg_dimensions;
return $svg_dimensions;
}