/** * Compatible with PHP getimagesize() * @todo support gzipped SVGZ * @todo check XML more carefully * @todo sensible defaults * * @param $filename String: full name of the file (passed to php fopen()). * @return array */ function wfGetSVGsize($filename) { $width = 256; $height = 256; // Read a chunk of the file $f = fopen($filename, "rt"); if (!$f) { return false; } $chunk = fread($f, 4096); fclose($f); // Uber-crappy hack! Run through a real XML parser. $matches = array(); if (!preg_match('/<svg\\s*([^>]*)\\s*>/s', $chunk, $matches)) { return false; } $tag = $matches[1]; if (preg_match('/\\bwidth\\s*=\\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches)) { $width = wfScaleSVGUnit(trim(substr($matches[1], 1, -1))); } if (preg_match('/\\bheight\\s*=\\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches)) { $height = wfScaleSVGUnit(trim(substr($matches[1], 1, -1))); } return array($width, $height, 'SVG', "width=\"{$width}\" height=\"{$height}\""); }
function filter($name, $attribs) { if ($this->first) { $defaultWidth = self::DEFAULT_WIDTH; $defaultHeight = self::DEFAULT_HEIGHT; $aspect = 1.0; $width = null; $height = null; if (isset($attribs['viewBox'])) { // min-x min-y width height $viewBox = preg_split('/\\s+/', trim($attribs['viewBox'])); if (count($viewBox) == 4) { $viewWidth = wfScaleSVGUnit($viewBox[2]); $viewHeight = wfScaleSVGUnit($viewBox[3]); if ($viewWidth > 0 && $viewHeight > 0) { $aspect = $viewWidth / $viewHeight; $defaultHeight = $defaultWidth / $aspect; } } } if (isset($attribs['width'])) { $width = wfScaleSVGUnit($attribs['width'], $defaultWidth); } if (isset($attribs['height'])) { $height = wfScaleSVGUnit($attribs['height'], $defaultHeight); } if (!isset($width) && !isset($height)) { $width = $defaultWidth; $height = $width / $aspect; } elseif (isset($width) && !isset($height)) { $height = $width / $aspect; } elseif (isset($height) && !isset($width)) { $width = $height * $aspect; } if ($width > 0 && $height > 0) { $this->width = intval(round($width)); $this->height = intval(round($height)); } $this->first = false; } }