Monday 1 Sep 2008

Write a text on an image

Here is the next part of the tutorial to include text in images. :-) To understand this, you must have read the basics on images in PHP with GD2.

Computer and PHP tutorials

Images in PHP with GD2 - Part 2 put some text on the image

The text

The basic function to add a text is imagestring which is used with imageloadfont to indicate the write font. But using these functions is less simple and portable than imagettftext which allows to load a TTF format font. So I will only talk about this last.
imagettftext (resource $image, float $size, float $angle, int $x, int $y, int $color, string $ttf_file, string $text)

$size is the font size in points, $angle is the angle in degree, $ttf_file is the font file to use. So just put this font with the rest of the site to see your script run on any server. :-)

Okay, now we have all the elements to set the text in a empty image. I will put everything as parameter, so you can have fun. This is the HTML code that inserts my image:
<img src=" text is modifiable!&couleur1=FFFFFF&couleur2=AADD44&couleur_fond=1F1F1F" alt="text in image" />

It gives:

texte en image

Here's the link to the image, try to modify the text and the colors, you will immediately see the useful aspect for the title in images.

:!: Keep in mind that this text won't have the SEO power of a text, but of an image! We can't have everything.

And here's finally the PHP code generating this little wonder. :-D

 * Text in image -
 * The variables to give are: l h texte1 texte2 couleur_texte1 couleur_texte2 couleur_fond
 * = width height text1 text2 text1_color text2_color background_color
header ('Content-type: image/png');
$im = imagecreate ($_GET['l'], $_GET['h']);
// or $im = imagecreatetruecolor ($_GET['l'], $_GET['h']);
$couleur_fond = imagecolorallocate ($im,
   hexdec(substr($_GET['couleur_fond'], 0, 2)),
   hexdec(substr($_GET['couleur_fond'], 2, 2)),
   hexdec(substr($_GET['couleur_fond'], 4, 2)));
// and if we choose imagecreatetruecolor, add the background with:
// imagefilledrectangle($im, 0, 0, $_GET['l'], $_GET['h'], $couleur_fond);
$couleur1 = imagecolorallocate ($im,
   hexdec(substr($_GET['couleur1'], 0, 2)),
   hexdec(substr($_GET['couleur1'], 2, 2)),
   hexdec(substr($_GET['couleur1'], 4, 2)));
$couleur2 = imagecolorallocate ($im,
   hexdec(substr($_GET['couleur2'], 0, 2)),
   hexdec(substr($_GET['couleur2'], 2, 2)),
   hexdec(substr($_GET['couleur2'], 4, 2)));
imagettftext ($im, 18, 10, 30, 45, $couleur1, './desyrel.ttf', stripslashes ($_GET['texte1']));
imagettftext ($im, 24, 0, 15, 65, $couleur2, './desyrel.ttf', stripslashes ($_GET['texte2']));
// Retire stripslashes if magic_quotes is disabled.

The .ttf file requires of course, to be the the .php file. ;-)

Finally I would remind you that the texts and codes on this site are protected by copyright. The goal is that you learn and not copy-paste my PHP code.

Have fun image

On occasion I will continue with the shapes drawing and modification of existing images or pictures. I will also talk about the error handling missing in this example. ;-)

cafĂ© Did this article help you? 
Buy me a coffee!

Leave a comment (all comments are moderated, don't waste time with spam)

Azure Dev