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->thumbn开发者_如何学PythonailImage(100, 0);
header( "Content-Type: image/jpg" );
echo $image;
?>
Does anyone have any suggestions?
Thanks!
I had to do the same thing with Youtube... you need to pass the file path to ImageMagic, not the file_put_contents instance.
<?php
$kunaki_image = file_get_contents("http://kunaki.com/ProductImage.ASP?T=I&ST=FO&PID=PX003Y9EDJ");
$name = tempnam("/tmp", "kunaki");
file_put_contents($name, $kunaki_image);
$image = new Imagick($name);
$image->thumbnailImage(100, 0);
header( "Content-Type: image/jpg" );
echo $image;
?>
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.
if you perfer cUrl,
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$resp = curl_exec($curl);
$image = new \Imagick();
$image->readImageBlob($resp);
$image->thumbnailImage(100, 0);
header( "Content-Type: image/jpg" );
echo $image;
a little improvement to avoid writing on the disk:
<?php
$handle = fopen("http://kunaki.com/ProductImage.ASP?T=I&ST=FO&PID=PX003Y9EDJ", 'rb');
$image = new \Imagick();
$image->readImageFile($handle);
fclose($handle);
$image->thumbnailImage(100, 0);
header( "Content-Type: image/jpg" );
echo $image;
精彩评论