3 * SVGMetadataExtractor.php
9 class SVGMetadataExtractor
{
10 static function getMetadata( $filename ) {
11 $filter = new XmlSizeFilter();
12 $xml = new XmlTypeCheck( $filename, array( $filter, 'filter' ) );
13 if( $xml->wellFormed
) {
15 'width' => $filter->width
,
16 'height' => $filter->height
23 const DEFAULT_WIDTH
= 512;
24 const DEFAULT_HEIGHT
= 512;
26 var $width = self
::DEFAULT_WIDTH
;
27 var $height = self
::DEFAULT_HEIGHT
;
28 function filter( $name, $attribs ) {
30 $defaultWidth = self
::DEFAULT_WIDTH
;
31 $defaultHeight = self
::DEFAULT_HEIGHT
;
36 if( isset( $attribs['viewBox'] ) ) {
37 // min-x min-y width height
38 $viewBox = preg_split( '/\s+/', trim( $attribs['viewBox'] ) );
39 if( count( $viewBox ) == 4 ) {
40 $viewWidth = $this->scaleSVGUnit( $viewBox[2] );
41 $viewHeight = $this->scaleSVGUnit( $viewBox[3] );
42 if( $viewWidth > 0 && $viewHeight > 0 ) {
43 $aspect = $viewWidth / $viewHeight;
44 $defaultHeight = $defaultWidth / $aspect;
48 if( isset( $attribs['width'] ) ) {
49 $width = $this->scaleSVGUnit( $attribs['width'], $defaultWidth );
51 if( isset( $attribs['height'] ) ) {
52 $height = $this->scaleSVGUnit( $attribs['height'], $defaultHeight );
55 if( !isset( $width ) && !isset( $height ) ) {
56 $width = $defaultWidth;
57 $height = $width / $aspect;
58 } elseif( isset( $width ) && !isset( $height ) ) {
59 $height = $width / $aspect;
60 } elseif( isset( $height ) && !isset( $width ) ) {
61 $width = $height * $aspect;
64 if( $width > 0 && $height > 0 ) {
65 $this->width
= intval( round( $width ) );
66 $this->height
= intval( round( $height ) );
74 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
75 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
77 * @param $length String: CSS/SVG length.
78 * @param $viewportSize: Float optional scale for percentage units...
79 * @return float: length in pixels
81 function scaleSVGUnit( $length, $viewportSize=512 ) {
82 static $unitLength = array(
89 'em' => 16.0, // fake it?
90 'ex' => 12.0, // fake it?
91 '' => 1.0, // "User units" pixels by default
94 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
95 $length = floatval( $matches[1] );
98 return $length * 0.01 * $viewportSize;
100 return $length * $unitLength[$unit];
104 return floatval( $length );