Add text on Image
-Image processing in PHP
Before you start this lesson, before read the lesson PHP
Image processing introduction and make sure you enable GD library.
Here is PHP code to add text on Image:
| <?php
// Import the image to use
$TheImage = imagecreatefromjpeg("pig.jpg");
//Choose the Color of the text...(255,0,0 is red)
$ColorText = imagecolorallocate($TheImage, 255, 0, 0);
// printing the text:.
imagettftext($TheImage, 10, 60, 25, 110, $ColorText, "Verdana", "Happy
Valentine");
// Let the browser know that it is an png image..
header("Content-Type: image/png");
// show the image in png format...(png format is clearer
imagepng ($TheImage);
?>
|
The function imagecolorallocate(...) choose color for the
text. Function imagettftext(...) writes text on image.
Explanation for each parameter in imagettftext($TheImage,
10, 60, 25, 110, $ColorText, "Verdana", "Happy
Valentine");:
- $TheImage : the image
- 10 : the font size. Depending on your version of GD, this
should be specified as the pixel size (GD1) or point size (GD2).
- 60 : The angle in degrees, with 0 degrees being left-to-right
reading text. Higher values represent a counter-clockwise rotation.
For example, a value of 90 would result in bottom-to-top reading
text.
- 25 and
110: define the upper-left corner of the first character.
- $ColorText: color of text
- "Verdana": text font;
- "Happy Valentine": is the text we want to write on image;
Processed image sample
Here is the sample of the above code:
|