Skip to content

Instantly share code, notes, and snippets.

@microweber
Last active December 17, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save microweber/5549593 to your computer and use it in GitHub Desktop.
Save microweber/5549593 to your computer and use it in GitHub Desktop.
<?php
function svgScaleHack($svg, $minWidth, $minHeight)
{
$reW = '/(.*<svg[^>]* width=")([\d.]+px)(.*)/si';
$reH = '/(.*<svg[^>]* height=")([\d.]+px)(.*)/si';
preg_match($reW, $svg, $mw);
preg_match($reH, $svg, $mh);
if(!isset($mw[2]) and isset($mh[2])){
$mw[2] = $mh[2];
}
if(empty($mw)){
$width = floatval($minWidth);
$height = floatval($minHeight);
} else {
$width = floatval($mw[2]);
$height = floatval($mh[2]);
}
if (!$width || !$height) return false;
// scale to make width and height big enough
$scale = 1;
if ($width < $minWidth){
$scale = $minWidth/$width;
}
if ($height < $minHeight){
$scale = max($scale, ($minHeight/$height));
}
$scale = 1;
//$width *= $scale*2;
//$height *= $scale*2;
$svg = preg_replace($reW, "\${1}{$width}px\${3}", $svg);
$svg = preg_replace($reH, "\${1}{$height}px\${3}", $svg);
return $svg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment