php-svg icon indicating copy to clipboard operation
php-svg copied to clipboard

Unexpected result when rasterizing svg image that include style.

Open lypwig opened this issue 4 years ago • 0 comments

I try to output a png image from a svg but I get unexpected behavior with the style I use.

PHP code sample

<?php
require_once ("./vendor/autoload.php");

use SVG\SVG;

$image = SVG::fromFile('./test.svg');

$doc = $image->getDocument();
$raster = $image->toRasterImage($doc->getWidth(), $doc->getHeight());
imagepng($raster);

input SVG source

<svg height="100" width="200">
	<style>
rect { fill: red; }
circle { fill: darkgray; }
.container circle { fill: green; }
.container rect { fill: blue; }
	</style>

	<circle cx="50" cy="50" r="40"/>
	<rect x="30" y="30" width="40" height="40"/>
	<g class="container">
		<circle cx="150" cy="50" r="40"/>
		<rect x="130" y="30" width="40" height="40"/>
	</g>
</svg>

Input SVG image

image

Output PNG image

image

Observations

  • the style defined directly on svg elements is not used;
  • the style defined for an element in a container is used by the whole container.

lypwig avatar Jun 22 '21 12:06 lypwig