title: PHP readImageBlob from SVG
body: <p>I am using Raphael JS client-side to render a chart as an SVG. However, I want the chart to be downloadable and SVG doesn't allow this. First, I tried rendering the svg as a png using  and a data uri:</p>
  
  <pre><code>var $svg = this.$chart.children("svg");
  var img = new Image();
  img.setAttribute("download", "chart.png");
  var that = this;
  img.onload = function() {
      that.$chart.append(img);
  };
  img.src = "data:image/svg+xml;utf8,"+$svg[0].outerHTML;
  </code></pre>
  
  <p>This didn't work because the images and gradients I used were not rendered.</p>
  
  <p>Now I am making an API call sending the SVG as POST data and trying to convert the SVG to a PNG using imagick. The SVG dump can be found <a href="https://gist.github.com/anonymous/46e098513cbc074b9b24" rel="nofollow">here</a> (for reference).</p>
  
  <p>Here is the PHP script I'm working on:</p>
  
  <pre><code>header('Content-Type: image/png');
  $svg = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?&gt;" . urldecode($_POST["imagedata"]);
  error_log($svg);
  $image = new IMagick();
  $image-&gt;setBackgroundColor(new ImagickPixel('transparent'));
  $image-&gt;readImageBlob(urldecode($svg));
  $image-&gt;setImageFormat("png32");
  echo $image-&gt;getImageBlob();
  </code></pre>
  
  <p>The above GIST is the output from the error_log call. When this script is called, I get a 500 error in the AJAX call and the PHP server gives this error:</p>
  
  <pre><code>\nFatal error: Uncaught exception 'ImagickException' with message 'Unable to read image blob' in :\nStack trace:\n#0 file.php(30): Imagick-&gt;readImageBlob()\n#1 {main}
  </code></pre>
  
  <p>I would appreciate help solving this is any number of ways. Better method to convert to an image, way to debug the "faulty" svg file (via error log of readImageBlob perhaps?), etc.</p>
  
  <p>Ultimately, I just don't have enough information about what went wrong with the readingImageBlob. The SVG is obviously correct... I guess. It renders correctly in the browser. I'm just not sure how to debug this read function.</p>