Skip to content

Instantly share code, notes, and snippets.

@slackero
Created June 5, 2020 11:25
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 slackero/dfb5028fd825ec3fa091365863a9c773 to your computer and use it in GitHub Desktop.
Save slackero/dfb5028fd825ec3fa091365863a9c773 to your computer and use it in GitHub Desktop.
Try to resize SVG in PHP
<?php
// https://stackoverflow.com/questions/48820832/how-to-resize-svg-with-php
$dom = new DOMDocument('1.0', 'utf-8');
$dom->load('/pathe/to/my/svg.svg');
$svg = $dom->documentElement;
if ( ! $svg->hasAttribute('viewBox') ) { // viewBox is needed to establish
// userspace coordinates
$pattern = '/^(\d*\.\d+|\d+)(px)?$/'; // positive number, px unit optional
$interpretable = preg_match( $pattern, $svg->getAttribute('width'), $width ) &&
preg_match( $pattern, $svg->getAttribute('height'), $height );
if ( $interpretable ) {
$view_box = implode(' ', [0, 0, $width[0], $height[0]]);
$svg->setAttribute('viewBox', $view_box);
} else { // this gets sticky
throw new Exception("viewBox is dependent on environment");
}
}
$svg->setAttribute('width', '1000');
$svg->setAttribute('height', '1000');
$dom->save('/pathe/to/my/new.svg');
@aaryadev
Copy link

Thanks, :) 👍

@mochamaddarmawanh
Copy link

Thanks, :) 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment