Write Text on to an existing PNG image using PHP
Published 2 years, 9 months ago in Image Handling and PHPThis article will tell you how to write text on an existing PNG image using PHP using PHP's image manipulation functions. The image functions require the GD library to be installed. This article is fairly basic, however some more detail on the basic functions used is included in the article, "write text on a dynamically generated image in PHP"
First, we need the source image. In this example, we're using an image called image.png (with dimension 150x150, though this code will work with any size of image) which is shown below:
This image is going to be kept in the same directory on the webserver as the php file we're using to manipulate it.
Now it's time for PHP code to load the image. We make use of the function imagecreatefrompng() to get the image into PHP to manipulate. This takes the path to the image as a parameter. If the image can't be loaded then we'll tell it to die() and stop interpreting the page, however there are better ways of dealing with this error.
-
$im = imagecreatefrompng("image.png");
-
// if there's an error, stop processing the page:
-
if(!$im)
-
{
-
}
Next, we'll define some colours for use in the image using the imagecolorallocate() function:
-
$red_background = imagecolorallocate($im, 255, 0, 0);
-
$black = imagecolorallocate($im, 0, 0, 0);
Now we get the dimensions of the image, using the imagesx() and imagesy(), which take the image resource as a parameter and return the width and height respectively:
-
// get the width and the height of the image
-
$width = imagesx($im);
-
$height = imagesy($im);
The next step is to draw a black rectangle on to the image to ensure that the text we write is visible. This step is optional, as you could write the text directly on to the image.
The coordinate system used in PHP is that the top left of an image is (0,0) and the bottom right is ($width, $height). We're going to draw the rectangle across the bottom 20px of the image, so we pass imagefilledrectangle two sets of coordinates: (0, $height-20) and ($width, $height). The coordinates we're passing - as the 2nd to 4th parameters) tell the method to draw a rectangle from a point on the left hand side, 20 pixels from the bottom, to the very bottom right of the image. The rectangle is then automatically filled black.
-
// draw a black rectangle across the bottom, say, 20 pixels of the image:
-
imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);
Now it's time to write the text on the image. In this example, we will write the text in the middle of the rectangle we just created. First we declare two variables, $font and $text, to store the int representing the system font we're using and the text we're going to write to the image, respectively.
-
$font = 4; // store the int ID of the system font we're using in $font
-
$text = "vdhri.net"; // store the text we're going to write in $text
Then we will calculate where the left edge of the text is. This is done by using imagefontwidth() to get the width of the system font we're using (which is fixed width, so all characters have the same width), then multiplying that by the number of characters in the string we're writing. This finds the length of the text in pixels. We then subtract that from the total width of the image and divide by two to find the left position, which we store in the variable $leftTextPos:
-
// calculate the left position of the text:
Now we write the text to the image, using the imagestring() function passing the variables we created earlier as parameters (NB. As height we're using the image height minus 18 pixels, to estimate a decent fit in the box, rather than calculating the height in the same way as width.)
-
// finally, write the string:
-
imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);
Lastly, we send the image to the browser then clear it from memory:
-
// output the image
-
// tell the browser what we're sending it
-
// output the image as a png
-
imagepng($im);
That's it! the output image is:
This will be shown in the browser whenever anyone accesses the php page we've just created!
To finish off, here's the code in full:
-
<?
-
// load the image from the file specified:
-
$im = imagecreatefrompng("image.png");
-
// if there's an error, stop processing the page:
-
if(!$im)
-
{
-
}
-
-
// define some colours to use with the image
-
$yellow = imagecolorallocate($im, 255, 255, 0);
-
$black = imagecolorallocate($im, 0, 0, 0);
-
-
// get the width and the height of the image
-
$width = imagesx($im);
-
$height = imagesy($im);
-
-
// draw a black rectangle across the bottom, say, 20 pixels of the image:
-
imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);
-
-
// now we want to write in the centre of the rectangle:
-
$font = 4; // store the int ID of the system font we're using in $font
-
$text = "vdhri.net"; // store the text we're going to write in $text
-
// calculate the left position of the text:
-
// finally, write the string:
-
imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);
-
-
// output the image
-
// tell the browser what we're sending it
-
// output the image as a png
-
imagepng($im);
-
-
// tidy up
-
imagedestroy($im);
-
?>
If you have a low-traffic website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!
9 Responses to “Write Text on to an existing PNG image using PHP”
Leave a Reply
You must log in to post a comment.
Search
Categories
- ASP (3)
- ASP Basics (3)
- Blogs (2)
- C# (1)
- deals (1)
- Firefox (1)
- Flash (1)
- Google (6)
- Hacks (4)
- Java (1)
- PHP (12)
- Basics (8)
- Files (1)
- Image Handling (3)
- Puzzles (1)
- Ruby (1)
- Tips (10)
- Web Hosting (3)
- Web Hosting Articles (1)
- Web Hosting News (2)
- Windows (6)
- Wordpress (2)
Related Entries
- PHP
- Write text on a dynamically generated image using PHP
- Rotating Images with PHP
- Google: Filter By Usage Rights
- Hide a hard drive from My Computer
- How to stop .avi files from locking up in Windows Explorer
- How to use the QueryString in ASP
- Generate a Month Calendar In PHP
- How to use the query string in PHP
- How To Add A New Line in a C# or Visual Basic TextBox
Thanks! Just what i needed
Cool, but how to make it split into 2 lines if it overflows?
Please show me an example, how with
$left $right $center $downleft $downright i could put the text in x, y place?
Thanks
I like to use an existing image (gif, 200×200 pixels) in a new image (1024×768) which I will draw with imagepng.
Can you please help me?
Thanks
Congratulation! Excellent tutorial!
Thanks,
Mirek
thnks
excellent.
I’ve wanted to do this for a long time but all the texts on it have been way too complex, yours had me making my own dynamic button within minutes.
a big thumbs up !!
How it possible to display multiple image from Mysql database?
Hi,
Very good guidelines.very helpful.
thanks
Roshith Kaniyamchlil