PHP function to draw wrapped text

Here’s a PHP code snippet that draws text bounded by a textbox using the GD library. It supports rotation, vertical and horizontal alignments, configurable line heights and leading, word wrap (obviously), character wrap, and text outlines. While this frankly belongs in a class (or a namespace) due its size, it’s in a function. You’re free to trim away unneeded functionality.

imagettftextboxopt() example

However, be aware that:

  • You should specify a line height, or text won’t appear consistently spaced with different source texts.
  • The alignment isn’t exactly pixel-perfect yet.
  • The function is best used for languages that have a space character.
  • To be honest, the code has some pretty bad alignment issues. It may or may not matter in your case. I would like to rewrite this from scratch, but that takes some time.

You can find the code snippet here:
http://www.sk89q.com/pastebin/php/imagettftextboxopt.phps

Example usage:

$size = 14;
$angle = 34;
$left = 10;
$top = 40;
$color = imagecolorallocate($im, 255, 0, 0);
$fontfile = "arial.ttf";
$text = "Test";
$opt = array(
    'width' => 300,
    'line_height' => 15,
    'orientation' => array(ORIENTATION_TOP, ORIENTATION_LEFT),
    'align' => ALIGN_LEFT,
    'v_align' => VALIGN_TOP,
    'outlines' = array(
        array(5, imagecolorallocate($im, 0, 255, 0)),
        array(2, imagecolorallocate($im, 0, 0, 255)),
    ),
    // More options documented in the code
);
imagettftextboxopt($im, $size, $angle, $left, $top, $color, $fontfile, $text, $opt);

Maybe in the future, I’ll implement this as a class, fix the small bugs, and allow for textbox overflow.

Ideally, you should use ImageMagick. It does this better and faster than any native PHP code can. It is also a better image manipulation library. Even if I were to fix up this code, it is not possible for me to handle particular font features such as kernings.


62 Responses to PHP function to draw wrapped text

  1. Amrish Prajapati

    Can i do text bold or italic with this ?


  2. die kann aber nicht von dir sein die ganze funktion da sind sehr viele fehler drin sogar auch im beispielcode der funktioniert noch nicht mal


  3. Hi,

    I’ve started using your piece of code (works very well, thanks!), but I keep running into a small problem concerning text color. It seems like the parameter color generated using:
    $color = imagecolorallocate($image, $r, $g, $b);
    is not taken into account, and the rendered text looks like the average color from the entire $image file.

    Any ideas on how to solve this?


    • Never mind.

      I was using a GIF image and the palette was full, so it could not process the imagecolorallocate correctly.

      Converted it to JPG and worked like a charm.


  4. The link for the code for imagettftextboxopt doesn’t work.

    Does it will work even with php 5.3?


  5. Is there any way of justifying the text both left and right sides? I mean, re-distribute the spaces between words so it coual be double justify.

    Thanks.


Leave a Reply

Your email address will not be published.

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