The only case you may need to set size information in the SVG file is if you display it as a standalone file (for example if you have a link pointing to the .svg itself).
In this case, you can take advantage of the fact that SVG is a XML file:
$dom = new DOMDocument('1.0', 'utf-8');
$dom->load('/pathe/to/my/svg.svg');
$svg = $dom->documentElement;
if ( ! $svg->hasAttribute('viewBox') ) {
$pattern = '/^(\d*\.\d+|\d+)(px)?$/';
$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 {
throw new Exception("viewBox is dependent on environment");
}
}
$svg->setAttribute('width', '1000');
$svg->setAttribute('height', '1000');
$dom->save('/pathe/to/my/new.svg');
There are some tricky cases if the element has no viewBox
attribute and the width and height have non-px units. Handling them requires a deeper understanding of SVG. It would be best to assure that the SVG files produced by the "other script" always have a viewBox
. Then the conditional section can be left out.
The term "resolution" is inapplicable for vector grafics. Width and height can be dimensionless numbers (representing pixel for the purpose of rendering into an output device, for example the screen), or any valid CSS length unit: em, ex, px, pt, pc, cm, mm, in, and percentages.