Scriptplayground

 Print Page | Close Window

Text On Image

Learn how to display text on an image using the GD Library in PHP.

This tutorial REQUIRES the GD Library to be installed in order to work.


Here is the code.

<?php
$image = ImageCreateFromPNG("base.png");
$color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66);
$font = 'arial.ttf';
$fontSize = "10";
$fontRotation = "0";
$str = "Example of GD in PHP.    Date: " . date("m-j-Y  g:i:s (a)");

/* Shadow */
ImageTTFText($image, $fontSize, $fontRotation, 7, 22, $colorShadow, $font, $str);

/* Top Level */
ImageTTFText($image, $fontSize, $fontRotation, 5, 20, $color, $font, $str);

header("Content-Type: image/PNG");
ImagePng ($image);
imagedestroy($image);
?>


/*******************************************************

$image is the background that the string is applied to
$color is the applied text's color 
$colorShadow is the color of the drop shadow
$font is the font loaded to create the image
$fontSize is the size of the font "imagine that" :)
$fontRotation is the amount the text will be rotated [default = 0]
$str is the text that is printed on the image

*******************************************************/

Here is an explanation on what each of the variables are.



$image = ImageCreateFromPNG("base.png");

This is the php function to create a PNG using a starter image. For this example the starter image is named "base.png"



// '0x' is placed before the value for a hex value otherwise you give an "RGB" value
$color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66);

This line is used to set the foreground & shadow of the text that we will render. The first parameter is calling the base image. The next 3 are the color values of the text and shadow. (ex: #333333 is changed to 0x33,0x33,0x33)



$font = 'arial.ttf';
$fontSize = "10";
$fontRotation = "0";

$font is the font file loaded to draw the text. You must upload that font file to your server in order for this example to work. $fontSize is the size of the rendered text. $fontRotation is the amount of rotation applied to the text.



$str = "Example of GD in PHP.    Date: " . date("m-j-Y  g:i:s (a)");

$str is the string(text) we will render to the PNG. For added coolness we will render the date and time.



/* Shadow */
ImageTTFText($image, $fontSize, $fontRotation, 7, 22, $colorShadow, $font, $str);

/* Top Level */
ImageTTFText($image, $fontSize, $fontRotation, 5, 20, $color, $font, $str);

'Top level' & 'Shadow' have most of the same parameters, the Difference is placement on the image. The first param is the image that is edited, the second param is the font size, the third param is the rotation of the text, the fourth param is the distance from the left side, the fifth param is the distance from the top



header("Content-Type: image/PNG");

This sends a command to the browser that says this filetype is PNG. This is set so the browser doesn't think it is a page.



ImagePng ($image);

This sends the image to the browser which sets the browser to display the new generated PNG.



imagedestroy($image);

After the image is displayed 'destroy' it to clear the buffer. This used a small piece of the buffer but every bit adds up.



That is the end of the tutorial. Now you should have a better understanding how to add text to an image.


Find this article at:
http://scriptplayground.com/tutorials/php/Text-On-Image/