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

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Post Reply
ivntheme
Posts: 3
Joined: 2019-01-11T00:35:44-07:00
Authentication code: 1152

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

Post by ivntheme » 2019-01-11T00:39:10-07:00

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!

snibgo
Posts: 11335
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo » 2019-01-11T06:54:18-07:00

You can create an image just for the text using "label:". If the result is wider than you want, resize it down.
snibgo's IM pages: im.snibgo.com

ivntheme
Posts: 3
Joined: 2019-01-11T00:35:44-07:00
Authentication code: 1152

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

Post by ivntheme » 2019-01-11T20:50:03-07:00

@snibgo I don't understand. Can you explain more :(

User avatar
fmw42
Posts: 24613
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2019-01-11T23:34:19-07:00

See https://imagemagick.org/Usage/text/#label. Sorry, I do not know the MagickWand equivalent.

snibgo
Posts: 11335
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo » 2019-01-12T11:51:12-07:00

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.
snibgo's IM pages: im.snibgo.com

ivntheme
Posts: 3
Joined: 2019-01-11T00:35:44-07:00
Authentication code: 1152

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

Post by ivntheme » 2019-01-13T19:25:58-07:00

many thanks!

Post Reply