Page 1 of 1

How to draw a long text to a image without wrapping it (using Imagick php)

Posted: 2019-01-11T00:39:10-07:00
by ivntheme
Hi everybody,
I want to draw text in a image, using Imagick and PHP7 (support by Hostgator). I have problem when the text is very long, i don't want to wrapping (new lines), I want to scale width of the text (height is a constant).

Here is a Demo that I want.
Image

Please tell me the solution.

My PHP code:

Code: Select all

if(isset($_POST["value1"])){
    $image = new Imagick('background.jpg');
$text = $_POST["value1"];
$draw = new ImagickDraw();
$draw->setFont('Arial.ttf');
$draw->setFontSize(25);
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
$height = $image->getimageheight(); 
$width = $image->getimagewidth(); 
$centerX = $width/2;
$centerY = $height/2;
$draw->setFillColor('#fcf59c');
$image->annotateImage($draw, $centerX, $centerY, 0, "This is a long text"); 
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
}
Many thanks!

Re: How to draw a long text to a image without wrapping it (using Imagick php)

Posted: 2019-01-11T06:54:18-07:00
by snibgo
You can create an image just for the text using "label:". If the result is wider than you want, resize it down.

Re: How to draw a long text to a image without wrapping it (using Imagick php)

Posted: 2019-01-11T20:50:03-07:00
by ivntheme
@snibgo I don't understand. Can you explain more :(

Re: How to draw a long text to a image without wrapping it (using Imagick php)

Posted: 2019-01-11T23:34:19-07:00
by fmw42
See https://imagemagick.org/Usage/text/#label. Sorry, I do not know the MagickWand equivalent.

Re: How to draw a long text to a image without wrapping it (using Imagick php)

Posted: 2019-01-12T11:51:12-07:00
by snibgo
What needs explaining? Create a new image for the text something like this:

Code: Select all

$textimage = new Imagick('label:This is a long text');
... and if that image is wider than you need then resize it down, then composite that over your main image.

I don't use PHP. Refer to documentation for the correct syntax etc.

Re: How to draw a long text to a image without wrapping it (using Imagick php)

Posted: 2019-01-13T19:25:58-07:00
by ivntheme
many thanks!