PHP ImageStringRight / Center / Italic


PHP’s built-in ImageString function is OK, but I often find it lacking some of the basic features you’d expect. So I’ve created ImageStringRight, ImageStringCenter, ImageStringItalic, and a few other goodies for writing text to images in PHP.

PHP ImageString Enhanced!

Image String Enhanced!

Demo and source code after the break!

Live Demo: PHP ImageString Demo

ImageString Source Code

The source contains code and usage examples for the following enhanced PHP Image String functions:

  • ImageStringRight: Creates a right-aligned string
  • ImageStringCenter: Creates a string centered on the x-axis
  • ImageStringUnderlined: Underlines ImageString!
  • ImageStringItalic: Write a string in italics onto an image
  • ImageStringShadow: Write a string with a dropshadow onto an image

They even work with fonts you load via ImageLoadFont, which is pretty awesome!

You can even nest effects, to create for instance, right-aligned italic strings with a drop shadow!

There’s an optional final parameter on each function, $ImageString, which contains a string representing the function to use to write the string. So for instance, if we want to write a right-aligned string in italics, we can use the following code:

ImageStringRight($image, 5, 25, 'Right + Italic', $col, 5, 'ImageStringItalic');

Where:

  • $image is the result of an ImageCreate or ImageCreateTrueColor call
  • 5 is the font to use
  • 25 is the y-offset (i.e. how far down the image the string is to be written)
  • $col is the color to write the text in
  • 5 is an offset from the right (otherwise the string will be right up against the edge of the image)
  • and finally we send “ImageStringItalic” as the ImageString function, so that ImageStringRight creates a right-aligned string using ImageStringItalic.

To nest more than two effects (eg right-aligned, italic and underlined), you’ll need to create a “helper” function, for instance like so:

function ImageStringItalicUnderlined($image, $font, $x, $y, $str, $col) {
        ImageStringItalic($image, $font, $x, $y, $str, $col, 2, 'ImageStringUnderlined');
}

Then you can use that helper function as the “$ImageString” parameter to ImageStringRight, like so:

ImageStringRight($image, 5, 10, 'Nested Effects', $red, 'ImageStringItalicUnderlined');

The PHP source code contains quite a few examples, but if you can’t get the effect you’re looking for just drop a comment here and I’ll help you out :D


Related Posts:

, , , , , , , , ,

  1. #1 by james on May 17, 2009 - 7:27 am

    hi,i used the gd for a online-diy system for my project,but i have found the gd do not support blod fonts,can you update your function,many thanks.
    kind regards.
    james from China.

    • #2 by user24 on May 18, 2009 - 4:24 am

      If you email the fonts to I’ll take a look at them and try to see why it’s not working.

      Thanks.

      • #3 by james on May 18, 2009 - 6:33 pm

        hi user24,i have sent a mail to you with the fonts i have,can you try it ,thanks very much.
        james.

  2. #4 by james on June 4, 2009 - 6:17 pm

    hi,do you have msn or other chat tools,here is my msn

    • #5 by user24 on June 8, 2009 - 1:54 pm

      Hi! I emailed you back but I guess you didn’t get it.

      The fonts you sent me were TTF fonts – my code only works with GD fonts.

      You can use TTF with PHP’s ImageTTFText function, but I don’t think there’s a way to do things like right-aligned text for TTF in PHP. I might give it a go when I have some spare time, but in the meantime, why not convert the TTFs to GDFs using something like this: http://www.wedwick.com/wftopf.exe

      I haven’t tested that .exe so check it for viruses before running it!

  3. #6 by james on June 8, 2009 - 5:56 pm

    thanks again
    i will check it first

  4. #7 by Niklas on September 9, 2009 - 9:19 am

    Hello…

    I like what you have done with the :
    PHP ImageStringRight / Center / Italic

    I´m wondering one thing, is there a way to make this work with only image.

    I have this script that makes thumbnail ..
    I have make so i can get a white background.

    $thumb = imagecreatetruecolor(120, 100);
    $white = imagecolorallocate($thumb, 255, 255, 255);
    imagefill($thumb, 0, 0, $white);

    But i whant to make the image that i create to be a thumb to be in the center of the white backgrund..
    Now the thumb is in the top or to the left…

    I hope that you have a idé how i can do…

    I have post the script i´m using in a swedish page.
    I dont think u understand swedish, but you can se the script.

    Best regards Niklas

  5. #8 by Niklas on September 9, 2009 - 9:21 am

    Hello…

    I like what you have done with the :
    PHP ImageStringRight / Center / Italic

    I´m wondering one thing, is there a way to make this work with only image.

    I have this script that makes thumbnail ..
    I have make so i can get a white background.

    $thumb = imagecreatetruecolor(120, 100);
    $white = imagecolorallocate($thumb, 255, 255, 255);
    imagefill($thumb, 0, 0, $white);

    But i whant to make the image that i create to be a thumb to be in the center of the white backgrund..
    Now the thumb is in the top or to the left…

    I hope that you have a idé how i can do…

    I have post the script i´m using in a swedish page.
    I dont think u understand swedish, but you can se the script.

    http://www.phpportalen.net/viewtopic.php?p=653577#653577

    Best regards Niklas

  6. #9 by aleks on December 15, 2009 - 6:05 am

    thanks for the very nice script it really helps me to add some new idea! :)

    regards,

    aleks

  7. #10 by Hemanathagiribabu on April 12, 2010 - 8:28 am

    It could be very useful to my IP Phone weather report generation as Image format.

  8. #11 by jeetendra kumar singh on October 10, 2011 - 11:48 am

    nice code
    it helped me so much
    thanks
    have any code for dotted underlined string
    if have you please send me

Comments are closed.