lib/blobfolio/common/image.php
public static function svg_dimensions($svg) {
		ref\cast::string($svg, true);

		// Make sure this is SVG-looking.
		if (false === ($start = \strpos(\strtolower($svg), '<svg'))) {
			if (@\is_file($svg)) {
				$svg = \file_get_contents($svg);
				if (false === ($start = \strpos(\strtolower($svg), '<svg'))) {
					return false;
				}
			}
			else {
				return false;
			}
		}

		// Chop the code to the first tag.
		$svg = \substr($svg, $start);
		if (false === ($end = \strpos($svg, '>'))) {
			return false;
		}
		$svg = \strtolower(\substr($svg, 0, $end + 1));

		// Hold our values.
		$out = array(
			'width'=>null,
			'height'=>null,
		);
		$viewbox = null;

		// Search for width, height, and viewbox.
		ref\sanitize::whitespace($svg, 0);

		\preg_match_all(
			'/(height|width|viewbox)\s*=\s*(["\'])((?:(?!\2).)*)\2/',
			$svg,
			$match,
			\PREG_SET_ORDER
		);

		if (\is_array($match) && \count($match)) {
			foreach ($match as $v) {
				switch ($v[1]) {
					case 'width':
					case 'height':
						ref\cast::float($v[3], true);
						ref\sanitize::to_range($v[3], 0.0);
						if ($v[3]) {
							$out[$v[1]] = $v[3];
						}
						break;
					case 'viewbox':
						// Defer processing for later.
						$viewbox = $v[3];
						break;
				}
			}
		}

		// If we have a width and height, we're done!
		if ($out['width'] && $out['height']) {
			return $out;
		}

		// Maybe pull from viewbox?
		if (isset($viewbox)) {
			$viewbox = \trim(\str_replace(',', ' ', $viewbox));
			$viewbox = \explode(' ', $viewbox);
			foreach ($viewbox as $k=>$v) {
				ref\cast::float($viewbox[$k], true);
				ref\sanitize::to_range($viewbox[$k], 0.0);
			}
			if (\count($viewbox) === 4) {
				$out['width'] = $viewbox[2];
				$out['height'] = $viewbox[3];
				return $out;
			}
		}

		return false;
	}
public static function svg_dimensions($svg) {
		ref\cast::string($svg, true);

		// Make sure this is SVG-looking.
		if (false === ($start = \strpos(\strtolower($svg), '<svg'))) {
			if (@\is_file($svg)) {
				$svg = \file_get_contents($svg);
				if (false === ($start = \strpos(\strtolower($svg), '<svg'))) {
					return false;
				}
			}
			else {
				return false;
			}
		}

		// Chop the code to the first tag.
		$svg = \substr($svg, $start);
		if (false === ($end = \strpos($svg, '>'))) {
			return false;
		}
		$svg = \strtolower(\substr($svg, 0, $end + 1));

		// Hold our values.
		$out = array(
			'width'=>null,
			'height'=>null,
		);
		$viewbox = null;

		// Search for width, height, and viewbox.
		ref\sanitize::whitespace($svg, 0);

		\preg_match_all(
			'/(height|width|viewbox)\s*=\s*(["\'])((?:(?!\2).)*)\2/',
			$svg,
			$match,
			\PREG_SET_ORDER
		);

		if (\is_array($match) && \count($match)) {
			foreach ($match as $v) {
				switch ($v[1]) {
					case 'width':
					case 'height':
						ref\cast::float($v[3], true);
						ref\sanitize::to_range($v[3], 0.0);
						if ($v[3]) {
							$out[$v[1]] = $v[3];
						}
						break;
					case 'viewbox':
						// Defer processing for later.
						$viewbox = $v[3];
						break;
				}
			}
		}

		// If we have a width and height, we're done!
		if ($out['width'] && $out['height']) {
			return $out;
		}

		// Maybe pull from viewbox?
		if (isset($viewbox)) {
			$viewbox = \trim(\str_replace(',', ' ', $viewbox));
			$viewbox = \explode(' ', $viewbox);
			foreach ($viewbox as $k=>$v) {
				ref\cast::float($viewbox[$k], true);
				ref\sanitize::to_range($viewbox[$k], 0.0);
			}
			if (\count($viewbox) === 4) {
				$out['width'] = $viewbox[2];
				$out['height'] = $viewbox[3];
				return $out;
			}
		}

		return false;
	}
public static function svg_dimensions ( $ svg ) { ref \ cast :: string ( $ svg , true ) ; // Make sure this is SVG-looking. if ( false === ( $ start = \ strpos ( \ strtolower ( $ svg ) , '<svg' ) ) ) { if ( @ \ is_file ( $ svg ) ) { $ svg = \ file_get_contents ( $ svg ) ; if ( false === ( $ start = \ strpos ( \ strtolower ( $ svg ) , '<svg' ) ) ) { return false ; } } else { return false ; } } // Chop the code to the first tag. $ svg = \ substr ( $ svg , $ start ) ; if ( false === ( $ end = \ strpos ( $ svg , '>' ) ) ) { return false ; } $ svg = \ strtolower ( \ substr ( $ svg , 0 , $ end + 1 ) ) ; // Hold our values. $ out = array ( 'width' => null , 'height' => null , ) ; $ viewbox = null ; // Search for width, height, and viewbox. ref \ sanitize :: whitespace ( $ svg , 0 ) ; \ preg_match_all ( '/(height|width|viewbox)\s*=\s*(["\'])((?:(?!\2).)*)\2/' , $ svg , $ match , \ PREG_SET_ORDER ) ; if ( \ is_array ( $ match ) && \ count ( $ match ) ) { foreach ( $ match as $ v ) { switch ( $ v [ 1 ] ) { case 'width' : case 'height' : ref \ cast :: float ( $ v [ 3 ] , true ) ; ref \ sanitize :: to_range ( $ v [ 3 ] , 0.0 ) ; if ( $ v [ 3 ] ) { $ out [ $ v [ 1 ] ] = $ v [ 3 ] ; } break ; case 'viewbox' : // Defer processing for later. $ viewbox = $ v [ 3 ] ; break ; } } } // If we have a width and height, we're done! if ( $ out [ 'width' ] && $ out [ 'height' ] ) { return $ out ; } // Maybe pull from viewbox? if ( isset ( $ viewbox ) ) { $ viewbox = \ trim ( \ str_replace ( ',' , ' ' , $ viewbox ) ) ; $ viewbox = \ explode ( ' ' , $ viewbox ) ; foreach ( $ viewbox as $ k => $ v ) { ref \ cast :: float ( $ viewbox [ $ k ] , true ) ; ref \ sanitize :: to_range ( $ viewbox [ $ k ] , 0.0 ) ; } if ( \ count ( $ viewbox ) === 4 ) { $ out [ 'width' ] = $ viewbox [ 2 ] ; $ out [ 'height' ] = $ viewbox [ 3 ] ; return $ out ; } } return false ; }
Determine SVG Dimensions

@param string $svg SVG content or file path.
@return array|bool Dimensions or false.
Determine SVG Dimensions