Current location: Hot Scripts Forums » Programming Languages » PHP » Creating Dynamic Images With PHP


Creating Dynamic Images With PHP

Reply
  #1 (permalink)  
Old 02-20-05, 04:26 PM
cdphreaker cdphreaker is offline
New Member
 
Join Date: Feb 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Creating Dynamic Images With PHP

Many people do not realize that PHP can be used to create non-HTML data. This is especially useful for creating images on the fly. It could be simple bar graphs that display data from a database, or even simpler, just a way to create graphic buttons on the fly.

The following code does exactly that... save it as button.php3

<?php
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="button.php3?s=36&text=PHP+is+Cool">

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.
You can do some very neat things with this. You can use just about any TTF font. If you have a Windows box sitting around, you can "borrow" any of the .TTF files in your Windows font directory, or look around on the Net on sites like the one above and download some. Have fun with it and if you do interesting things with it, let people here know.

Same post can be here
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Script to Insert Dynamic Txet into Jpg image cyborlink PHP 3 05-05-05 06:26 AM
PHP upload images without FTP? AlexLee PHP 3 12-25-04 01:41 AM
Creating a Zip file with PHP dannyallen PHP 3 07-01-04 09:47 AM
Creating pages with php 1jetsam PHP 2 04-15-04 09:19 PM
error when creating database tables with php script spiroth10 PHP 4 01-06-04 04:59 PM


All times are GMT -5. The time now is 01:14 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.