| ★ wanayoo — archive 1999 http://www.phpbuilder.com/columns/rasmus19990124.php3 | Nouvelle recherche | Portail wanayoo |
|
|
|||
|
|
|
|
||
|
|
|
Creating Dynamic Images with PHPRasmus Lerdorf
When I am putting up a site quickly, I find that it is a waste of my time to sit and fiddle with an image editor to create nice-looking graphical buttons and menus. Instead I will pick a nice TTF font and use the following simple script which I usually call 'button.php3': <?Header( "Content-type: image/gif"); if(!isset($s)) $s=11; $size = imagettfbbox($s,0, "/fonts/TIMES.TTF",$text); $dx = abs($size[2]-$size[0]); $dy = abs($size[5]-$size[3]); $xpad=9; $ypad=9; $im = imagecreate($dx+$xpad,$dy+$ypad); $blue = ImageColorAllocate($im, 0x2c,0x6D,0xAF); $black = ImageColorAllocate($im, 0,0,0); $white = ImageColorAllocate($im, 255,255,255); ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black); ImageRectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white); ImageTTFText($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "/fonts/TIMES.TTF", $text); ImageTTFText($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "/fonts/TIMES.TTF", $text); ImageGif($im); ImageDestroy($im); ?> It is very important to realize that you cannot put any HTML tags in this file. There should also not be any spaces or blank lines before or after the <? and ?> tags. If you are getting a broken image using this script, chances are you have a stray carriage return somewhere outside the PHP tags. The above script would be called with a tag like this from a page: <IMG SRC=/old?u=http%3A%2F%2Fwww.phpbuilder.com%2Fcolumns%2F%26quot%3Bbutton.php3%3Fs%3D36%26text%3DPHP%2Bis%2BCool%26quot%3B%26gt%3B%3C%2FTT&y=1999>
And it looks like this: The 's' argument sets the font size and the button auto-scales itself to match. Here it is again with s=18:
Note that I draw first a black rectangle and then a white rectangle slightly offset to create a 3D look. This might be a little hard to see on a light background, but you would adjust your colours appropriately. And then I do the same with the actual font. I first draw a black version and then slightly offset draw a white version of the string on top. You can of course play around with the spacing and colours to get the exact effect you want. Ok, so how can you do this on your site? You need to make sure that your PHP setup has both GD and TTF support. See the PHP FAQ for links to these libraries. It can be a little tricky getting both GD and TTF support compiled into PHP. I suggest copying libgd.a to /usr/local/lib and the gd*.h files to /usr/local/include and just do a standard 'make install' for the FreeTTF library which should stick the appropriate files under /usr/local/lib and /usr/local/include. If you are on Solaris and using the shared library version of libttf, then you may see an error when you build Apache that says that it can't find libttf.so. If this is the case, edit the src/modules/php3/libphp3.module file and just before the "-lttf" add -R/usr/local/lib -L/usr/local/lib. Make sure you do a 'make clean' before trying to rebuild Apache and things should work ok.
|
||
|
|
|
Sample Code | Columns | Mail Archive | Support | Get Started! | Links | Contribute!
Contact (non support questions) By viewing these pages you agree to the Legal Terms of Service.
Geocrawler.com | GoToCity.com | DirectriCity.com | PHPBuilder.com | The Des Moines City.net |