Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@aaryadev aaryadev commented Jul 11, 2021

Thanks, :) 👍

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