Skip to content

Instantly share code, notes, and snippets.

@innermond
Created November 2, 2021 08:29
Show Gist options
  • Save innermond/bbc634bd25f32295b56f20f18fe7671e to your computer and use it in GitHub Desktop.
Save innermond/bbc634bd25f32295b56f20f18fe7671e to your computer and use it in GitHub Desktop.
scale a svg - width, height, x, y
function scale($svg, $k) {
$rxvbox = '/viewBox="(\d+) (\d+) (\d+) (\d+)"/si';
$rxw = '/width="(\d+)"/si';
$rxh = '/height="(\d+)"/si';
$rxx = '/x="(\d+)"/si';
$rxy = '/y="(\d+)"/si';
$svg = preg_replace_callback($rxvbox, function($m) use ($k) {return 'viewBox="'.$m[1]*$k.' '.$m[2]*$k.' '.$m[3]*$k.' '.$m[4]*$k.'"';}, $svg);
$svg = preg_replace_callback($rxw, function($m) use ($k) {return 'width="'.$m[1]*$k.'"';}, $svg);
$svg = preg_replace_callback($rxh, function($m) use ($k) {return 'height="'.$m[1]*$k.'"';}, $svg);
$svg = preg_replace_callback($rxx, function($m) use ($k) {return 'x="'.$m[1]*$k.'"';}, $svg);
$svg = preg_replace_callback($rxy, function($m) use ($k) {return 'y="'.$m[1]*$k.'"';}, $svg);
return $svg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment