I am sending a string representation of an SVG file to the server and using Imagick to turn this into a jpeg in the following manner:
$image = stripslashes($
As a workaround of php_imagick's bug, you can scale svg's width=".." and height="..":
function svgScaleHack($svg, $minWidth, $minHeight)
{
$reW = '/(.*
Then you can easily create nice transparent PNG!
createThumbnail('a.svg', 'a.png');
function createThumbnail($filename, $thname, $size=50)
{
$im = new Imagick();
$svgdata = file_get_contents($filename);
$svgdata = svgScaleHack($svgdata, $size, $size);
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImageBlob($svgdata);
$im->setImageFormat("png32");
$im->resizeImage($size, $size, imagick::FILTER_LANCZOS, 1);
file_put_contents($thname, $im->getImageBlob());
$im->clear();
$im->destroy();
}
Note: I've been searching for a solution how to rescale SVG from its initial small size. However it seems that imagick::setResolution is broken. However, ImageMagick library itself is working, so you can use exec('convert...') (might be disabled for security reasons by hosting provider).
So to create thumbnail 50x50 from smaller svg you would do:
convert -density 500 -resize 50 50 -background transparent a.svg PNG32:a.png