0

I found an imagemagick convert command here to fit text into a smaller box, namely

convert sea.jpg \( -size 173x50 -background none label:"A" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png

I'm trying to replicate this in PHP using the IMagick library However, I can't seem to find a way to add a label? The IMagick LabelImage function doesn't actually add a label on the image but only the metadata.

my current attempt got me so far:

    $label = new Imagick();
    $label->newImage(173,50,new ImagickPixel('transparent'),"png");
    $label->labelImage("Testing 123");
    $label->trimImage(0);
    $label->setGravity(imagick::GRAVITY_CENTER);

However the labelImage, as said, doesn't actually add anything to the image, so I think I've hit a dead end. What would be a good way to add a label to an image, or in general are there better ways to add text to an image in a shrink-to-fit way in a box using the IMagick library?

Thanks in advance :)

jurrejelle
  • 31
  • 3

1 Answers1

0

I haven't used IMagick for ages, and will delete this if incorrect, but I guess you probably want newPseudoImage with label: as the type. Sorry I can't test this, but something like:

label = Imagick::newPseudoImage ( 173, 50, "label:The text you want")
Composite over your background
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I tried it, with some code from example 1 of [here](https://www.php.net/manual/en/imagick.newpseudoimage.php),and made a file that does nothing more than create an image, add a pseudoimage and return it. however that doesn't seem to work with label? It works with the pseudo string "plasma:fractal" which gives a pretty image, but "label:Test" doesn't work – jurrejelle Feb 11 '21 at 13:40
  • Maybe you could add your latest/greatest code into your question? Click the `edit` button under it to get started. – Mark Setchell Feb 11 '21 at 14:12