PHP ImageMagick 从 http 获取图像

标签 php http imagemagick

我正在尝试使用 ImageMagick 进行一些照片处理(暂时裁剪)。我以前通过使用 PHP 的 GD 库(在我的本地主机上)获得了预期的结果,但现在托管不支持它,他们建议我改用 ImageMagick。问题是我可以裁剪存储在本地的图像:

<?php
$four = '4fingers1.jpg';
exec("convert $four -crop 100x100+100+100 test.jpg");
?>
<img src="test.jpg">

没问题,但是当它来自 http 源(我的脚本的实际目的)时,我没有收到图像。代码如下(我用表格发布了图片的实际源代码):

$src = $_POST['src'];
exec("convert $src -resize 720x720 resized.jpg"); // this is specified in the documentation
exec("convert resized.jpg -crop 100x100+100+100 final.jpg");
?>
<img src="final.jpg"> <!-- no image -->
<img src="resized.jpg"> <!-- no image -->
<img src="<?php echo $src; ?>"> <!-- alright -->

文档指定以下内容:

IM can also download an image that is published on the 'world wide web' by specifying that images URL. This basically provides a 'http:' image coder, which is why it works.

链接:http://www.imagemagick.org/Usage/files/#read

我已经尝试了很多在线创建的代码片段,但无法弄清楚为什么不起作用?我可以使用其他东西来达到我的目的吗?

最佳答案

首先,作为囚犯comments此代码容易受到 shell 参数注入(inject)的攻击。基本上任何人都可以操纵 post 参数并使您执行他们想要的任何命令行; 这可能是灾难性的,应立即解决!

解决安全问题的一种方法是使用 escapeshellarg ;这也应该解决您的“原始”问题,因为没有明显的命令失败原因。如果 URL 有效并且参数被正确转义,它应该可以正常工作。

或者,您可以通过自己下载图像并在调用 IM 之前将其保存到临时文件来解决安全问题和您原来的问题:

$temp = tempnam(sys_get_temp_dir());
file_put_contents($temp, file_get_contents($url));

exec("convert ".escapeshellarg($temp)." -crop 100x100+100+100 test.jpg");

关于PHP ImageMagick 从 http 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15765100/

相关文章:

rest - 当请求需要先完成另一个请求时,我可以使用 HTTP 响应 424 吗?

angularjs - POST 请求 - Firebase - 预检响应无效(重定向) - CORS

Wcf Http 和 Https

c++ - 使用 Magick++ 和 openMPI 编译 C++ 代码

php - 设置ImageMagick PNG图像背景颜色

php - Magento - 在 1.8 上删除 "ShortDescription"Div

php - 使用 PHP(或查询)在 MySQL 数据库中使用超过数千行的常用词

php - 我如何将寻呼机提供给 drupal_add_tabledrag

php - mysql 锁定问题

php - ImageMagick 缓存资源耗尽