» Birthday Bundle - Over $400 worth of Envato files for just $20

PHP: How to center a text on an image using GD

Posted on September 9, 2008, Filled under PHP,  Bookmark it

Hello coders,

This is a snippet which centers a text on an image using the GD Library.

First, we create the image using the ImageCreate() function. Here we set the width & height.

$width = 400;
$height = 100;

$im = ImageCreate($width, $height);

The we use ImageColorAllocate() to fill the image with white background and set the border’s color, which is eventually created with ImageRectangle().

// white background and blue text
$bg = ImageColorAllocate($im, 255, 255, 255);

// grey border
$border = ImageColorAllocate($im, 207, 199, 199);
ImageRectangle($im, 0, 0, $width - 1, $height - 1, $border);

Set the text and its color:

$text = 'This is my photo description text.';

$textcolor = ImageColorAllocate($im, 0, 0, 255);

Now we need to setup the font size and determine the font height and width.

// Font Size
$font = 3;

$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);

In the next lines we determine the text’s width and height. Then, based on the information we have, the X, Y coordonates of the upper left corner can be calculated.

/*
-----------
Text Width
-----------
*/

$text_width = $font_width * strlen($text);

// Position to align in center
$position_center = ceil(($width - $text_width) / 2);

/*
-----------
Text Height
-----------
*/

$text_height = $font_height;

// Position to align in abs middle
$position_middle = ceil(($height - $text_height) / 2);

After we determine the coordinates, we the text using ImageString():

/*
-----------------
Write the string
-----------------
*/

$image_string = ImageString($im, $font, $position_center, $position_middle, $text, $textcolor);

And, finally, our last lines will output our image:

/*
--------------------------------------
Output our image (PNG in our example)
--------------------------------------
*/
header("Content-type: image/png");
ImagePNG($im);

Here’s the complete code:

<?php
/*
Credits: BitRepository
URL: http://www.bitrepository.com/web-programming/php/how-to-center-a-text-on-an-image-using-gd.html
*/

$width = 400;
$height = 100;

$im = ImageCreate($width, $height);

// white background and blue text
$bg = ImageColorAllocate($im, 255, 255, 255);

// grey border
$border = ImageColorAllocate($im, 207, 199, 199);
ImageRectangle($im, 0, 0, $width - 1, $height - 1, $border);

$text = 'This is my photo description text.';

$textcolor = ImageColorAllocate($im, 0, 0, 255);

// Font Size
$font = 3;

$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);

/*
-----------
Text Width
-----------
*/

$text_width = $font_width * strlen($text);

// Position to align in center
$position_center = ceil(($width - $text_width) / 2);

/*
-----------
Text Height
-----------
*/

$text_height = $font_height;

// Position to align in abs middle
$position_middle = ceil(($height - $text_height) / 2);

/*
-----------------
Write the string
-----------------
*/

$image_string = ImageString($im, $font, $position_center, $position_middle, $text, $textcolor);

/*
--------------------------------------
Output our image (PNG in our example)
--------------------------------------
*/
header("Content-type: image/png");
ImagePNG($im);
?>

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

4 Replies to "PHP: How to center a text on an image using GD"

  1. Shouldn’t you have a imagedestroy($im); at the end? PHP doesn’t discard old image objects, I believe, and that could cause trouble.

  2. Thanks for sharing this. I was looking for the best option to center an image. This helps.

  3. Your are a ninja. Sweet bra.

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)