简体   繁体   中英

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 :

  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). On the surface the mapping from command line to PHP seems rather straightforward, but somehow it's not. 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. Furthermore, there is no swap function.

What is the equivalent PHP code for adding a caption on top of the image?

[edit]

It seems labelImage just sets a property, just like commentImage (example provided this time). There is a render command that looks promising, but gives an error: 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:

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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