Using PHP and GD to add border to text

[code lang="PHP"] /**
* Writes the given text with a border into the image using TrueType fonts.
* @author John Ciacia
* @param image An image resource
* @param size The font size
* @param angle The angle in degrees to rotate the text
* @param x Upper left corner of the text
* @param y Lower left corner of the text
* @param textcolor This is the color of the main text
* @param strokecolor This is the color of the text border
* @param fontfile The path to the TrueType font you wish to use
* @param text The text string in UTF-8 encoding
* @param px Number of pixels the text border will be
* @see http://us.php.net/manual/en/function.imagettftext.php
*/
function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {

for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
$bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);

return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
} [/code]

[code lang="PHP"] $img = imagecreatefrompng("/home/john/Desktop/test.png");
$font_color = imagecolorallocate($img, 0, 0, 0);
$stroke_color = imagecolorallocate($img, 255, 0, 0);
imagettfstroketext($img, 10, 0, 10, 50, $font_color, $stroke_color, "abstract.ttf", "Hello, World!", 2);
?>[/code]

Related posts:

  1. imagettftext only displays yellow text Lately I have been doing some work with the PHP GD library. I wanted to put text on an image...
  2. Getting to know GD Create dynamic userbars with GD Introduction: Have you ever been on aim and received a message from one of your...
  3. LOL Monday – Videos I found this video hilarious. For those of you who are “slow,” it is how comment systems work on blogs....
  4. Binary to Decimal Conversion Converts a binary number to its decimal equivalent. However unlike the bindec function, this will preserve the binary/radix point. Code...
  5. Basic Logic The modern computer is perhaps one of the most complex and perplexing yet ever so simple creations known. The entire...

Leave a Reply

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>

Notify me of followup comments via e-mail. You can also subscribe without commenting.


5 Responses to “Using PHP and GD to add border to text”

  1. dylan

    Beautiful. I’ve been searching for a particular font for hours but this let me bypass my search. I should have thought of this myself! I’m using it for a meme generator that I’m making, and it will be live in a few days on Imgflip.com if you care to check it out.

    Reply |

    • John

      Dylan, I’m glad you found it useful. I’m looking forward to seeing our meme generator.

      Reply |

  2. WP Themes

    Good brief and this post helped me alot in my college assignement. Gratefulness you on your information.

    Reply |

  3. aneildo

    so good

    it helped me a lot

    thanks

    Reply |