0

I'm trying to create an jpg with Imagick but I have an error in readImageBlob

$image = new \Imagick();
$chart = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $chart;
$image->readImageBlob( $chart );
$image->setImageFormat("jpeg");

It says:

negative or zero image size `/tmp/magick-29893mIHq2qQrLKKP' @ error/image.c/SetImageExtent/2601

Pointing to the line that I mentioned before. I have previously defined $chartas:

$chart = '<svg xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" class="highcharts-root" style="font-family:"lucida grande", "lucida sans unicode", arial, helvetica, sans-serif;font-size:12px;" xmlns="http://... (ETC) ...g></svg>';

Could be a problem with how is $chart defined? What and how should I look for $chartin order to see if is correct defined or not?

The problem is reading a Blob, not converting a svg to a blob

pmiranda
  • 7,602
  • 14
  • 72
  • 155
  • `Imagick->readImageBlob` expects **binary data** http://php.net/manual/en/imagick.readimageblob.php – Charlotte Dunois Mar 03 '17 at 14:03
  • So, $chart contains a svg, how can handle it then with Imagick to read it? – pmiranda Mar 03 '17 at 14:05
  • Possible duplicate of [How to read an SVG with a given size using PHP Imagick?](http://stackoverflow.com/questions/9226232/how-to-read-an-svg-with-a-given-size-using-php-imagick) – Charlotte Dunois Mar 03 '17 at 14:07
  • 1
    If you run the image (plus the prefix you're adding) through a validator does it validate? – apokryfos Mar 03 '17 at 14:08
  • @apokryfos how can I do that? that's what I'm trying to do – pmiranda Mar 03 '17 at 14:11
  • Now I'm using `readImage($chart)`and I get: Invalid filename provided – pmiranda Mar 03 '17 at 14:12
  • Try using https://validator.nu/ – apokryfos Mar 03 '17 at 14:13
  • `readImage()`expects an URL of an image? Is there a way to pass it a php var that contains my svg? – pmiranda Mar 03 '17 at 14:14
  • I downloaded an SVG from http://www.highcharts.com/demo/line-time-series (using the export functionality) and the file includes a DOCTYPE and the xml version is already in there. Are you sure you're exporting the file correctly? – apokryfos Mar 03 '17 at 14:16
  • Well, I was following this: http://stackoverflow.com/questions/24903461/how-to-read-svg-string-in-imagick that's why I was using readImageBlob first, and my svg is like that example – pmiranda Mar 03 '17 at 14:20
  • Argh... the problem is with the formart of my avg, the $chart. My code works ok using other svg's, and I HAVE to use radImageBlob – pmiranda Mar 03 '17 at 14:25
  • @apokryfos by the way, the svg is "The document validates according to the specified schema(s)." – pmiranda Mar 03 '17 at 15:22

1 Answers1

0

Well, I was using readImageBlob with a svg file. I made this to read a binary:

//Set 2 temp files, one for the svg that I'll make and another one for the image that I will use later
$filesvg = '/tmp/chart.svg';
$chartImage = '/tmp/tempchartimg.jpg';

//the svg had to use that header
$chart = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>' . $chart;

//I'll save the svg in the temp file made before
file_put_contents($filesvg,$chart);
//Coverting and save in as 'binaryImage' file the svg to jpg
$binaryImage = shell_exec("convert $filesvg jpg:-");

//The the usal Imagick part:
$image = new \Imagick();

//Now I can read a binary file 
$image->readImageBlob($binaryImage);
$image->setImageFormat("jpg");
$image->setImageCompressionQuality(90);
$image->resizeImage(600, 400, \imagick::FILTER_LANCZOS, 1);
$image->writeImage($chartImage);

//rest of my code...
pmiranda
  • 7,602
  • 14
  • 72
  • 155