Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering an SVG file to a PNG or JPEG in PHP [duplicate]

Tags:

php

svg

gdlib

I've googled as much as I can, but I've only found a PHP class that calls upon Inkscape to render the SVG, so I ask here:

I have a valid SVG file generated in some way (or uploaded by a client). I need to render this into a JPG or PNG using just PHP and/or GDLib, as SVG is not supported by all browsers.

I do not have the option of installing anything, so a class that converts SVG to PNG using GDLib would be the most ideal.

like image 223
DanRedux Avatar asked Apr 23 '12 23:04

DanRedux


1 Answers

Check if ImageMagick is installed (you can find out using phpinfo). If it is, you can use the following code to cover to a PNG.

$image = new Imagick();
$image->readImageBlob(file_get_contents('image.svg'));
$image->setImageFormat("png24");
$image->resizeImage(1024, 768, imagick::FILTER_LANCZOS, 1); 
$image->writeImage('image.png');

There are many threads that discuss this. One that is particularly useful is this thread: Convert SVG image to PNG with PHP

like image 104
David Z. Avatar answered Nov 05 '22 08:11

David Z.