PHP:需要将imagemagick命令转换为php脚本 [英] PHP: Need to convert imagemagick command to php script
问题描述
我使用此命令批量修改站点的图像。他们需要符合这个主题的大小。
I use this command to batch modify images for a site. They need to be this size to suit the theme.
现在我也想在Facebook广告中使用这些图片,但必须调整它们的大小。我使用的命令(工作正常)是:
Now I also want to use these images in Facebook Ads, but they have to be resized. The command I use (which works fine) is:
for i in `ls -1 *.jpg`; do convert $i -fuzz 10% -trim +repage -resize 980x1200 -background white -gravity center -extent 980x1200 $i; done
现在我需要创建一个执行相同操作的PHP脚本,但也会将图像作为响应返回。
Now I need to make a PHP script that does the same, but also returns the image as response.
我想出了以下内容:
<?php
/* Create the object and read the image in */
$im = new Imagick("image.jpg");
/* Trim the image with 10% fuzz */
$im->trimImage(10);
/* Repage */
$im->setImagePage(0, 0, 0, 0);
/* Resize */
$im->resizeImage(1200,628,Imagick::FILTER_LANCZOS,0);
/* Add Borders*/
$im->setImageBackgroundColor('White');
$im->setGravity('Centre');
$im->setImageExtent(1200,628);
/* Output the image */
header("Content-Type: image/" . $im->getImageFormat());
echo $im;
?>
不幸的是它似乎不起作用。它只是返回一个黑色矩形(看起来像脚本中使用的正确尺寸)。
Unfortunately it doesn't seem to work. All it does is return a black rectangle (which looks like the right dimensions as used in the script).
推荐答案
第一个错误我运行此代码时获取:
First error I get when running this code:
PHP警告:Imagick :: setgravity()期望参数1为整数,在resize.php中给出字符串第18行
PHP Warning: Imagick::setgravity() expects parameter 1 to be integer, string given in resize.php on line 18
请尝试使用 imagick :: GRAVITY_CENTER
。
下一期, Imagick :: resizeImage()
和 Imagick :: setImageExtent()
期望参数的宽度,高度顺序。
Next issue, Imagick::resizeImage()
and Imagick::setImageExtent()
expect parameters in width, height order.
最后,尝试在 Imagick ::上为模糊设置一个非零值,如1 resizeImage()
解决黑色图像问题。
Finally, try setting a non-zero value like 1 for blur on Imagick::resizeImage()
to resolve the black image issue.
我不确定你是如何试图获得边框的,但你可能想要看看 Imagick :: borderImage()
。
I'm not sure how you are trying to get a border, but you may want to look at Imagick::borderImage()
.
我不知道这是否会解决你所有的问题,但它应该让你更接近!
I don't know if that will solve all your problems, but it should get you a lot closer!
<?php
/* Create the object and read the image in */
$im = new Imagick("image.jpg");
/* Trim the image with 10% fuzz */
$im->trimImage(10);
/* Repage */
$im->setImagePage(0, 0, 0, 0);
/* Resize */
$im->resizeImage(628, 1200, Imagick::FILTER_LANCZOS, 1);
/* Add Borders*/
$im->setImageBackgroundColor('White');
$im->setGravity(Imagick::GRAVITY_CENTER);
$im->setImageExtent(628, 1200);
/* Output the image */
header("Content-Type: image/" . $im->getImageFormat());
echo $im;
?>
这篇关于PHP:需要将imagemagick命令转换为php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!