ABOO
ABOO

Reputation: 145

GD Lib - Text with background over image

This one is quite difficult to explain clearly. So I think pictures might help.

I currently have a script that adds some text over an image in a custom font. I now want make it so that the text has a rectangular background to sit on.

The code I currently have is:

        $rImg = ImageCreateFromJPEG($_REQUEST['imageurl']);
        $cor = imagecolorallocate($rImg, 255, 255, 255);
        $font = 'font.TTF';
        imagettftext($rImg, 34, 0, 0, 100, $cor, $font, urldecode('Test Text'));
        header('Content-type: image/jpeg');
        imagejpeg($rImg,NULL,100);

It creates an image like this:

enter image description here

I need the image to look something like this:

enter image description here

Does anyone know a simple way of achieving this look? The red rectangle needs to end within range of the image. I would just use CSS, but it's for email!

Appreciate any advice!

Upvotes: 0

Views: 108

Answers (1)

Dexa
Dexa

Reputation: 1651

First you should calculate the boundaries of the text, so you can know how big red box you need. You could achive this with imagettfbbox function. Once you know the dimensions you can draw rectangle using imagefilledrectangle function. Also you should draw rectangle before you draw text.

Upvotes: 1

Related Questions