PHP imagetextblock Object

Text: Text to display
Size: Pixel size of text
Align: Left Center Right
Top Middle Baseline Bottom
Angle: Rotation in degrees
Color: Color in Hex (#0000FF) or RGB(0,0,255)
Opacity: Opacity (format: 100% or 1.0)

 

When generating images with PHP, proper placement of text can be difficult. The imagettftext function has served me well for various projects, but falls short when I need my text rotated, centered, right justified, etc. I used to find the bounding box every time, and use that to calculate a new position. However, this gets tedious, and makes for some overly-complex code. Thus the imagetextblock object was created.

The imagetextblock object makes rendering graphical text easier in a few different ways: Allowing the programmer to easily align the text horizontally or vertically, and specify colors in a more intuitive fashion. Rotation is also more intuitive, rotating around the point it's aligned to. Most importantly, the code is easier to understand when viewed later.

Rather than go on and on about the various features and how they work, I've created this demo to show how the various variables and functions work and how they affect the final image. Below you will find the source code to the imagetextblock object, and some sample code generated by the demo. Feel free to adjust values in the form at the left and see how the image changes. If you would like to use this in your projects, please credit me by at least leaving the credits at the top of the imagetextblock class file. Have any questions, comments, or anything else? Feel free to Contact Me.

class_imagetextblock.php

Sample Code: