8

I want to convert SVG into PNG using ImageMagick using PHP. I have installed ImageMagick on XAMPP and verified it using phpinfo(), but still can't generate images. Here is my code:

$svg = file_get_contents($svg_file);
//echo $svg;
$im = new Imagick();    
//$im->setBackgroundColor(new ImagickPixel('transparent'));  
// $svg = str_replace(array("color1","color2"),array("red","lightblue"),$svg);
$im->readImageBlob($svg);
//$im->setImageFormat("png32");
$im->setImageFormat("png24");
// $im->resizeImage(720, 445, imagick::FILTER_LANCZOS, 1);  
// $im->adaptiveResizeImage(720, 445);    
$im->writeImage($png_file);
header('Content-type: image/png');
echo $im;
$im->clear();
$im->destroy();
dario
  • 5,021
  • 12
  • 27
  • 32
Badar
  • 1,370
  • 1
  • 13
  • 19

3 Answers3

5

Read this Imagick on Windows 8 xampp

I make this example and works for me, just download the blank-us-map.svg

<?php

$usmap = 'blank-us-map.svg';
$im = new Imagick();
$svg = file_get_contents($usmap);

$im->readImageBlob($svg);

$im->setImageFormat("png24");
$im->resizeImage(720, 445, imagick::FILTER_LANCZOS, 1);  /*Optional, if you need to resize*/

$im->writeImage('blank-us-map.png');

header('Content-type: image/png');
echo $im;

$im->clear();
$im->destroy();

?>
Community
  • 1
  • 1
Adrian Cid Almaguer
  • 9,598
  • 13
  • 48
  • 65
  • @Badar then you don't have the extension installed, install the php5-imagick extension – Adrian Cid Almaguer May 02 '15 at 07:08
  • I had installed ImageMagick lib an extension in a folder /php/ext/imagick and also set it in php.ini, but still getting error. If you have a smooth and simpler way of installing ext then please point me to it. Thanks – Badar May 02 '15 at 07:31
  • @Badar I use linux and I just write in the shell sudo apt-get install php5-imagick and sudo service apache2 restart – Adrian Cid Almaguer May 02 '15 at 07:33
  • Ah, I am working on Windows 8 with Xampp. Followed tuts on web for installing imagemagick, but don't know why is this happening. – Badar May 02 '15 at 07:36
  • @Badar I don't know, try to search some info in google about this, I a little tired in my country is 3:42am and I go to sleep in some minutes – Adrian Cid Almaguer May 02 '15 at 07:38
  • php.ini shows ImageMagick installed, but when I load any page it displays "Fatal error: Class 'Imagick' not found in C:\dev\xampp\htdocs\polygon.php on line $Imagick = new Imagick();". – Badar May 02 '15 at 08:44
  • @Badar try to find your php5-imagick.dll in your xampp folder and copy to system32, I read that sometimes this work, In my case I see in the output of phpinfo() the name imagick not ImageMagick, take a look to this – Adrian Cid Almaguer May 02 '15 at 15:27
  • I followed your instructions, but still can't make php recognize imagemagick is installed in ext folder. – Badar May 03 '15 at 06:21
  • I followed your link, but still can't make XAMPP recognize imagick. Still getting Fatal error: Class 'Imagick' not found in $Imagick = new Imagick(); – Badar May 03 '15 at 07:07
  • I don't have access to Linux server right now. I think my only problem is installation of imagemagick dll in ext folder. Because Imagick class can't be recognized. – Badar May 03 '15 at 07:17
  • @Badar it seem that this is the problem, but I don't use Windows and I can't test the scenario, good luck with your problem, and if you solve the problem post the answer – Adrian Cid Almaguer May 03 '15 at 07:19
  • not that it matters too much, but the link to the svg is broke if that is something you want to change... – Shmack Nov 07 '20 at 18:51
1

It may not be related to your problem, but have you tried to call the Imagick class with a "\" before ? Like : $newImage = new \Imagick();

I know that I had the same error as you, that is the class couldnt be found, until I added this prefix namespace. I think its related to namespace and how you load your class files with the classLoader of your web-app.

Franz
  • 55
  • 4
0

If you get blank png images even though you are following all the advice here, you may want to check that any text within your svg is suitably escaped. I spent an hour or two scratching my head as to why I wasn't getting any output, until I discovered that I had an ampersand in my text. The fix was to pass the text through htmlspecialchars() first, i.e.

$annotation = htmlspecialchars($this->sequence . ' : ' . $this->name);
$svg .= "<text id=\"annotation-{$this->index}\" x=\"{$this->xText}\" y=\"{$this->yText}\" font-size=\"12\" style=\"fill: #000000\" onclick=\"passClickToTask(evt)\"> $annotation</text>";

Hope this helps anyone else facing similar problems.

RossD
  • 1
  • 1