Search code examples
phpimageimagemagickimagick

How to embed remote image in ImageMagick


I'm trying to do a simple thumbnail generation from an image that isn't located on my server using the iMagick wrapper for ImageMagick. For some reason, the following code will not display anything when called:

<?php
   $image = new Imagick("http://kunaki.com/ProductImage.ASP?T=I&ST=FO&PID=PX003Y9EDJ");
   $image->thumbnailImage(100, 0);
   header( "Content-Type: image/jpg" );
   echo $image;
?>

I've also tried using http://kunaki.com/ProductImage.ASP?T=I&ST=FO&PID=PX003Y9EDJ.jpg to no avail.

Based on comments below, I've attempted this as well with no results, but am not sure if the syntax is correct.

<?php
   $kunaki_image = file_get_contents("http://kunaki.com/ProductImage.ASP?T=I&ST=FO&PID=PX003Y9EDJ");
   $name = tempnam("/tmp", "kunaki");
   $final = file_put_contents($name, $kunaki_image);
   $image = new Imagick($final);
   $image->thumbnailImage(100, 0);
   header( "Content-Type: image/jpg" );
   echo $image;
?>

Does anyone have any suggestions?

Thanks!


Solution

  • ImageMagick's constructor is badly documented so I can't tell for sure, but maybe Imagick can't deal with remote file paths.

    Try fetching it separately e.g. using file_get_contents() or curl. Store it locally under a temporary name, and pass it that.