如何在 ImageMagick 中嵌入远程图像

发布于 2024-09-27 10:53:20 字数 872 浏览 8 评论 0原文

我正在尝试使用 ImageMagick 的 iMagick 包装器从不在我的服务器上的图像生成简单的缩略图。由于某种原因,以下代码在调用时不会显示任何内容:

<?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;
?>

我也尝试过使用 http://kunaki.com/ProductImage.ASP?T=I&ST=FO&PID=PX003Y9EDJ.jpg无济于事。

根据下面的评论,我也尝试过这一点,但没有结果,但不确定语法是否正确。

<?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;
?>

有人有什么建议吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小ぇ时光︴ 2024-10-04 10:53:20

我必须对 Youtube 做同样的事情...您需要将文件路径传递给 ImageMagic,而不是 file_put_contents 实例。

<?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;
?>

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;
?>
泛泛之交 2024-10-04 10:53:20

ImageMagick 的构造函数记录不完整所以我不能确定,但也许 Imagick 无法处理远程文件路径。

尝试单独获取它,例如使用 file_get_contents()curl 。将其以临时名称存储在本地,然后将其传递。

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.

不如归去 2024-10-04 10:53:20

如果您喜欢 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;

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;
弱骨蛰伏 2024-10-04 10:53:20

进行一点改进以避免在磁盘上写入:

<?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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文