简体   繁体   English

如何将ImageMagic Shell命令转换为等效的PHP脚本?

[英]How can I convert an ImageMagic shell command to an equivalent PHP script?

I have the following shell script that I copied almost verbatim from the ImageMagick manual : 我有以下shell脚本,几乎是从ImageMagick手册中逐字复制的:

  convert input.jpg -background White -pointsize 32 label:'Hello world' +swap  -gravity Center -append output.jpg;

Now I want to run the same operation on a shared webhost (that does not seem to have the convert command available). 现在,我想在共享的虚拟主机上运行相同的操作(似乎没有可用的convert命令)。 On the surface the mapping from command line to PHP seems rather straightforward, but somehow it's not. 从表面上看,从命令行到PHP的映射似乎很简单,但实际上并非如此。 The below code prints the input image unchanged. 下面的代码不变地打印输入图像。

//header('Content-type: image/jpeg');

$image = new Imagick('input.jpg');

$image->setBackgroundColor("White");
$image->setPointSize(32);
$image->setGravity(imagick::GRAVITY_CENTER);
$image->labelImage("hello world");
echo $image->count(); // 1
echo $image->appendImages(True);
//echo $image;

The labelImage function has very little documentation, and does not seem to do much at all. labelImage函数的文档很少,并且似乎根本不起作用。 Furthermore, there is no swap function. 此外,没有swap功能。

What is the equivalent PHP code for adding a caption on top of the image? 在图像顶部添加标题的等效PHP代码是什么?

[edit] [编辑]

It seems labelImage just sets a property, just like commentImage (example provided this time). 似乎labelImage只是设置了一个属性,就像commentImage (这次提供的示例)一样。 There is a render command that looks promising, but gives an error: Fatal error: Call to undefined method Imagick::render() . 有一个render命令看起来很有希望,但会给出错误: Fatal error: Call to undefined method Imagick::render() Odd... 奇...

I don't know about label, but the second approach from the linked documentation using splice does translate over nicely: 我不知道标签,但是链接文档中使用splice的第二种方法确实可以很好地转换:

$image = new Imagick("memes/$name.jpg");
$draw = new ImagickDraw();

$image->setBackgroundColor("White");
$image->spliceImage(0, 40, 0, 0);

$draw->setFillColor('black');
$draw->setFontSize( 28 );
$draw->setGravity(Imagick::GRAVITY_NORTH);
$image->annotateImage($draw, 10, 10, 0, $label);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM