14

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.

DanRedux
  • 9,119
  • 6
  • 23
  • 41
  • good solution using pure gd library https://www.phpclasses.org/browse/file/226624.html but it has some notice and warning that you need to fix. i use it in my project after fixing those notice and warning. hope it may help someone – Shahadat Hossain Khan Mar 18 '21 at 06:27

1 Answers1

30

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

Googlebot
  • 15,159
  • 44
  • 133
  • 229
David Z.
  • 5,621
  • 2
  • 20
  • 13
  • 2
    Sadly, it's not, and I can't install it. All I have is PHP and GDLib. It MUST be possible with GDLib.. It's almost just a translation, there's a GDLib function for almost every SVG tag. – DanRedux Apr 23 '12 at 23:39
  • GD doesn't support SVG. Wikimedia supports *ImageMagik*, *sodipodi*, *inkscape*, *batik*, *rsvg*, *imgserv*. If you can't install anything else, maybe you can see if those converters can run standalone and you can invoke it from your PHP script. – David Z. Apr 23 '12 at 23:48
  • good solution using pure gd library https://www.phpclasses.org/browse/file/226624.html but it has some notice and warning that you need to fix. i use it in my project after fixing those notice and warning. hope it may help someone – Shahadat Hossain Khan Mar 18 '21 at 06:27