Advertisement
mystream

svg-handling-wordpress

Apr 23rd, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. <?php
  2. function bodhi_svgs_get_dimensions( $svg = '') {
  3.  
  4.     // Default Response
  5.     $response = (object)[
  6.         'width' => '0',
  7.         'height'=> '0'
  8.     ];
  9.  
  10.     // Where should this file be
  11.     $template_uri   = wp_upload_dir();
  12.     $svg_basename   = basename($svg);
  13.     $svg_file       = $template_uri . '/' . $svg_basename;
  14.  
  15.     // Is this file where we think it should be?
  16.     if(file_exists($svg_file) && is_readable($svg_file)) {
  17.  
  18.         // It's supposed to be an SVG; can we load it like one?
  19.         $svg = @simplexml_load_file( $svg );
  20.  
  21.         // A really deep SVG may break and return false, even if valid
  22.         // But assuming it's not too deep, and can be read, then let's go
  23.         if (false !== $svg) {
  24.             $attributes         = $svg->attributes();
  25.             $response->width    = (string)$attributes->width;
  26.             $response->height   = (string)$attributes->height;
  27.         }
  28.     }
  29.  
  30.     return $response;
  31.  
  32. }
  33. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement