///Valentine's Day Fun with PHP and GD

Valentine's Day Fun with PHP and GD

Some of you may be familiar with Paul Reinheimer’s funcaday.com. Every day, the site lists a new PHP function, including the function prototype, description, uses and further notes. On Valentine’s Day this year, they chose a rather amusing parody function. Read on to have some fun with PHP and GD.

Some of you may be familiar with Paul Reinheimer’s funcaday.com. Every day, the site lists a new PHP function, including the function prototype, description, uses and further notes. On Valentine’s Day this year, they chose a rather amusing parody function:

Of course, while we’re all for accompanying our Valentine’s Day roses with personalised messages, why not have some fun with GD? The funcaday.com team have cleverly put together this page, where you can enter two names to be substituted in this example for $partnerA and $partnerB, as well as the var_dump(isLove()) section. For example:

So, how could you achieve this effect on your own? First you’ll need GD2, the PHP image library that provides various functions for manipulating images. You’ll then need to create a template with all the placeholders of your image but without the text. Remember that if the text being inserted is very long, it will have to be wrapped.

Use imagecreatefrompng() to create your canvas, followed by imagettftext() to write the text to the image (You’ll want to use TrueType fonts, as they’re anti-aliased). Finally, imagepng() will encode the image back to PNG and output it to the browser. You may already be familiar with this approach from CAPTCHA generation routines, most of which use the GD image library.

Go forth, experiment with GD2, and have a happy Valentine’s Day!

2010-05-19T23:06:07+00:00 February 14th, 2008|PHP|0 Comments

About the Author:

Leave A Comment