GD Lib - Text with background over image

ABOO

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!

Dexa

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related