473,408 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ


Generating Images with PHP

By Blair Ireland
Senior Editor, TheScripts.com

Dynamic Images in PHP 3.0

Yikes! Don't run if you feel threatened by the sounds of this, by the end of this lesson I'll have you making those images in no time.

PHP is definitely not limited to just standard html output, as you can tell by the title of this article, it can also output nice and clean graphics. All that is required on your server is, well, PHP of course, and the GD library of image functions. GD is available at http://www.boutell.com/gd/, and PHP is available at http://www.php.net/.

One limitation of this though is that you may only use the built-in fonts of the GD library. They aren't that horrible looking, just they are too limiting as there are only a few. No true type fonts here too..... There may be hope though. A true type font library has been made, and is usable on pretty much all systems. You may find it at http://www.freetype.org/.

Anyway, back to Dynamic images....

Modifying Already Made Images:

Ok, you must have a basic template for the graphic you want to have first of all, well, only if you are modifying already made images, but just goes without saying. This method is extremely handy for navigation bars and such. Usually you use the same basic image, but only the text is different. Therefore, this saves you some of that precious stuff all web developers are lacking, time.

Save the following as navbutton.php3 and edit where required.

<?

Header("Content-Type: image/gif");

$string=implode($argv," ");

$myimage = ImageCreateFromGif("images/yourimage.gif");

$black = ImageColorAllocate($myimage, 0, 0, 0);

$width = (imagesx($myimage)-7.5*strlen($string))/2;

ImageString($myimage, 1, $width, 20, $string, $black);

ImageGif($myimage);

ImageDestroy($myimage);

?>

This sort of image would be then called from something like <img src="navbutton.php3?texthere" border="0">

Cool? Yes. Convenient? Very. Why does TheScripts.com still not use them? Stupid GD fonts.... thats the only freakin reason. If we are able to get the GD graphics library working with truetype fonts, yes, TheScripts.com will be made of these dynamic images.

The ImageString function works with the following syntax;

ImageString($theimagevariable, the font number being used, x co-ordinate, y co-ordinate, The String to be inserted, The Color of the text);

The Upper Left co-ordinate of the image is (0,0), and works its way in from there. Our image is on the y co-ordinate 20 and the width is determined by the length of the string of text we are using.

It is also possible to create images from scratch using PHP and the GD graphics library, but to be honest, these graphics will never replace a graphic designer. They just do not look good.... and if they ever do, its usually a fluke. In turn, I would highly recommend you keep your current graphic designer and just use this great PHP function for simple graphics, like navigation bars.

One more note though before I send you off. The beauty of this function is that you don't really have to understand how it works if I lost you a while back, all you really have to do is get the relative idea of how this thingy works. After that, it should be easy enough for you.

 

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.