title: How to load an image resource from a URL into a PHP script, using cURL?
body: <p>I'm using the Google Charts service to generate some QR codes that I afterward need to manipulate (e.g. rotate, scale) in a PHP script and merge with other images to generate one final image.</p>
  
  <p>How do I correctly load such a resource (from a URL) into a PHP script, in a way that will allow me to manipulate it?</p>
  
  <p>An example URL is: <a href="https://chart.googleapis.com/chart?cht=qr&amp;chs=500x500&amp;chl=xghsdfgsdfg&amp;choe=UTF-8&amp;chld=L%7C0" rel="nofollow">https://chart.googleapis.com/chart?cht=qr&amp;chs=500x500&amp;chl=xghsdfgsdfg&amp;choe=UTF-8&amp;chld=L|0</a></p>
  
  <p>I currently have the following code to retrieve the image using cURL:</p>
  
  <pre><code>function getImage($url){
          $ch = curl_init ($url);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
          $resource = curl_exec($ch);
          curl_close ($ch);
  
          return $resource;
  }
  </code></pre>
  
  <p>But when I use it like this:</p>
  
  <pre><code>$image = imagecreatefrompng(getImage("https://chart.googleapis.com/chart?cht=qr&amp;chs=500x500&amp;chl=xghsdfgsdfg&amp;choe=UTF-8&amp;chld=L|0"));
  </code></pre>
  
  <p>The following error is returned:</p>
  
  <pre><code>Warning: imagecreatefrompng(‰PNG  ) [function.imagecreatefrompng]: failed to open stream: No such file or directory in /home/picselbc/public_html/projects/cakemyface/preview.php on line 383
  https://chart.googleapis.com/chart?cht=qr&amp;chs=500x500&amp;chl=xghsdfgsdfg&amp;choe=UTF-8&amp;chld=L|0
  </code></pre>