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 »

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: 12159
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 »

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 »

@snibgo I don't understand. Can you explain more :(
User avatar
fmw42
Posts: 25562
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 »

See https://imagemagick.org/Usage/text/#label. Sorry, I do not know the MagickWand equivalent.
snibgo
Posts: 12159
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 »

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 »

many thanks!
Post Reply