Προς το περιεχόμενο

Creating Dynamic Images with PHP


sotris99

Προτεινόμενες αναρτήσεις

Δημοσ.

Βρήκα αυτόν τον οδηγό αλλά δεν μου παίζει η εικόνα...

 

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.

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':

 

><?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">

 

And it looks like this: 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: PHP is cool in small text

 

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. This one is just a variation on the above script where I draw a single black letter on a white background. The font itself is called "Riot Act" and I found it on http://rover.wiesbaden.netsurf.de/~kikita/. 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.

 

Τι κάνω λάθος?

Δημοσ.

h deuterh gramh prepei na ginei :

print ("Content-type: image/gif");

 

kai meta tha deis oti den exeis th function imagettfbbox()

ekei pou vrikes auto to kodika prepei na sou dinei kai th function

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...