Centering Text in PHP/GD Images
Learn how to center your text drawn on images using the GD library and PHP.
Category: Coding
Views: 11582

Centering Text in PHP/GD Images

In this tutorial, I will show you a short and simple algorithm for centering text on a GD image produced by PHP. At first it may look large and complex, but if you read it slowly, it is quite simple and logical.

Here is a live example of this algorithm in action.

To start off, I'll just show you the base code of the centering of the text, without any other functions to actually create the image.

imagefontwidth($font_size))/2), 1), round(($img_h/2)-(imagefontheight($font_size)/2)), 
$text, $text_color);
?>

Whoa! What the heck does all of that mean? Well, let's take this one byte at a time (lol).

To start off, we should try to figure out what it is exactly that we want to do. In order to center the text horizontally, we want to take the total size of the image, then divide it by half. Then, we will need to find the width the actual text takes up in the image, divide it by half, and subtract that quantity from the halved image width. This will give us perfectly centered text if you can imagine this. Here are images showing practically what I just said.

Figure 1

Figure 2

Figure 3

Alright, so let's start piecing our little equation together.

First off, we want to start with the basics of the function, without our little centering bit.

We'll start with aligning horizontally. We'll start off by finding the width of the iamge, and dividing it in half, as shown in Figure 1.

$img_w/2

Now let's fund the width the font takes up. In order to get this, we can use the function strlen() with the $text variable, then multiply it by the width the font takes up, which can be found with the imagefontwidth() function. This will give us the total width of the text. Then, we will divide this in half, since we want to take this and subtract it from the image's halved width in order to completely center it.

(strlen($text)*imagefontwidth($font_size))/2

Now, we can combine this with the image's halved width, by subtraction. We will then use the round() function to round our result to the nearest whole number.

round(($img_w/2)-((strlen($text)*imagefontwidth($font_size))/2), 1)

And now you have perfectly centered text... horizontally! Now that you have this, you should be able to figure out how to make it centered vertically as well. Basically the exact same equation, only using the heights instead of the widths, and with no need to use the strlen() function.

round(($img_h/2)-(imagefontheight($font_size)/2))

And that's all! Your text rendered by PHP and the GD library is perfectly centered on your image! For convenience, you can even make this into an easy to use function that can be called the exact same way a normal imagestring() function would be called, only without the coordinants.

$img_w = imagesx($image);
$img_h = imagesy($image);
imagestring($image, $font_size, round(($img_w/2)-((strlen($text)*
imagefontwidth($font_size))/2), 1), round(($img_h/2)-(imagefontheight($font_size)/2)),
$text, $text_color);
}
?>

Cheers!

If you would like more help with this tutorial please register and visit the forum.

About Michael

Michael Dunlop Michael Dunlop personal statement

“But I am already older than 21”, I hear you cry. Don’t worry, despite the name, the information and advice is more geared towards retiring prior to having a fake hip installed.

Website Sponsors
Recommended Websites
They Buy, Build, Upgrade and Sell Websites.
 
Advertise Here
Feature Interviews
Graham went from $100 one day to ... Read more
Paul Bourque started affiliate marketing less than ... Read more
Interview with Yaro Starak of Entrepreneurs-Journey.com is ... Read more
Recommended Resources
 
 
 
 
 
Get Retire at 21's FREE Secrets
Find out how I'm going to Retire VERY Early Subscribe for my Free Secrets Privacy Guaranteed. 100% Spam free.