E-commerce Lead PHP Develoepr
WHI Solutions
US-CA-Valencia

Justtechjobs.com Post A Job | Post A Resume

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.
[ Next Page ]


Comments:
Safer codingTagg09/29/07 19:27
Creating Screen ShotsBinoy09/20/05 01:07
Creating Screen ShotsBinoy09/20/05 00:53
Multi-line text imagesClaudiu08/17/05 19:06
code (long!) RE: dynamic corners jisha07/24/05 01:30
GD give me rubishHåkan07/10/05 17:38
on teh fly img from text..Babul04/01/05 23:23
How can i make watermark with 24bit?goph-R03/17/05 07:56
RE: Creating thumbnails from mpeg files to giCreating thumbnails from mpeg files to gif03/01/05 07:35
Install Error perl.hGary02/21/05 10:14
RE: images with PHPZahid02/07/05 06:20
RE: Include into an htm (ERROR)Uday07/31/04 07:15
RE: GD library...impossible!Elliot Woods06/22/04 14:19
GD Support for Mac UsersDaniel Edwins06/03/04 09:35
GD on PHP4.3.3 and WindowsJayapraaksh11/07/03 03:53
RE: Acquiring FontsDaniel06/21/03 09:58
RE: getimagesize()wayne03/04/03 17:12
RE: GD not initializingRichard KS02/18/03 08:55
questionhitokakibs11/20/02 14:44
RE: GD not initializingExperttek11/18/02 23:30
RE: WARNING! include php(jpegimage) in htmNick11/14/02 16:12
Pls tell me how could i know image is stored?Khishigbaatar sosor10/23/02 02:51
getimagesize()peter03009/29/02 10:39
ENCODAGEarthur09/24/02 12:47
WARNING! include php(jpegimage) in htmstefan09/12/02 05:20
Include into an htm (ERROR)stefan09/12/02 05:15
RE: Acquiring Fontsdave09/09/02 16:59
How about a new variable for TTF?Leon09/05/02 12:12
RE: Acquiring FontsDan09/01/02 21:17
RE: TIFF supportRodrigo08/27/02 22:21
RE: TIFF supportcjpbob08/24/02 21:43
TIFF supportRodrigo08/17/02 15:47
RE: How to use image to create a HR using PHPcjpbob08/15/02 10:22
random number creationAngel08/08/02 11:23
How to use image to create a HR using PHPBabra T08/06/02 08:08
how come like thishong lipp ko08/02/02 13:36
RE: GD on Windows and PHP 4.06alex07/31/02 12:21
RE: Signup formcjpbob07/26/02 10:51
RE: Puting a text message on lot of imagescjpbob07/26/02 10:44
Dynamic Image Creation Helpcjpbob07/26/02 10:39
How change a couleur in a gif ?Florent07/16/02 16:46
RE: I get an "empty" .gifrobby07/16/02 08:53
Signup formTommy07/13/02 10:42
GD on Windows and PHP 4.06Cheekireddy Hari Kishore Reddy07/13/02 00:44
Intall JPEGLucas Leusin07/09/02 10:40
Browser not sending complete URL for images?Jay Raney07/05/02 10:22
PNG Image Support on NetPravish K Viswanathan06/21/02 06:42
GD is not workigSaeed Ahmad06/21/02 02:44
ImageTTFtextDiego Escalante06/12/02 09:47
RE: About GD libraryDiego Escalante06/12/02 09:46
Does gd support Hanyu (Chinese)?vwsoft06/07/02 03:47
GD &TTFVivek05/27/02 07:19
ADDING GD &TTF LIBRARY SUPPORTVivek05/25/02 13:18
GD&TTF Support for PHP4.0Vivek05/25/02 13:11
RE: GD librarydoujw05/18/02 21:35
pb with compiling php with GD and jpeg formatsebastien Pétré05/16/02 05:25
Invalid number of colors in paletteShek05/13/02 02:58
GD libraryriccardo05/11/02 10:31
How does PHP 4.2 impact this method?John Voth05/09/02 19:46
How to do this in PNGTotally05/09/02 16:11
RE: image creation from batabase stored imagesvictor ponz05/08/02 15:12
Manipulating graphics with PHPMichael Monahan04/25/02 23:29
RE: GD on Windows and PHP 4.06Chaz04/23/02 10:42
Image Creation while using HTML-TemplatesThomas Schaefer04/13/02 13:10
Image arrayDr-Leech03/28/02 10:40
Calling a single imageBrian03/19/02 21:09
RE: I get an "empty" .gifAnke02/15/02 11:40
gif doesn't workAdam02/12/02 16:14
RE: Creating a image that changes (jpg) plz helpRadhakrishna02/12/02 05:38
RE: GD on Windows and PHP 4.06mcq01/31/02 18:21
RE: Get the Binary Datafriz01/20/02 12:03
RE: image creation from batabase stored imagesGerald Alter01/18/02 11:46
Small box only displaying - THINGS TO CHECKGerald Alter01/18/02 00:25
RE: Rasmus or anyone...Gerald Alter01/17/02 21:25
Not working, I know install is correctGerald Alter01/17/02 18:04
RE: PHP - ImageMac01/12/02 21:22
HangingMac01/12/02 21:19
RE: error: GD don't build in PHP for windowsspoerri01/11/02 11:25
PHP - Imageegon01/09/02 14:48
Antialiased lines with gd?BitterClarence01/07/02 12:16
error: GD don't build in PHP for windowsAitor Soto01/04/02 08:35
RE: I get an "empty" .gifJLi12/27/01 10:41
RE: Transparant PNGDavid Beck12/14/01 15:37
Transparant PNGJoeri Cornelissens12/14/01 08:54
Invalidate charactersJean-Marie12/10/01 13:16
RE: Immediate help needed for image,..pleaseKawagator12/04/01 06:44
Puting a text message on lot of imagesNinad12/04/01 01:45
AnimateSteven Chalker12/03/01 17:16
Immediate help needed for image,..pleaseChandra11/28/01 04:31
image creation from batabase stored imagesagni11/24/01 23:26
GD library...impossible!Carl-David11/19/01 14:08
Include other pictures in your script...Rolf11/17/01 06:53
imageColorAllocate in JPEG [incorrect colors]LeChuck11/16/01 08:56
jpeg from .dbfChritoz11/11/01 16:35
How to get php-gd.so for linux?Shorn Mo11/07/01 08:53
Preventing image cachingIcarusTheVirus11/06/01 01:42
RE: It need GD library! But I can not use it!HELPAniruddha Deshpande11/03/01 23:38
RE: creating PNG from TIFF or EPS?francois10/28/01 10:55
Creating a image that changes (jpg) plz helpJamie10/27/01 11:10
RE: GD on Windows and PHP 4.06archerv10/25/01 02:54
GD on Windows and PHP 4.0 Same Problemarcherv10/25/01 00:05
GD on Windows and PHP 4.06Saurabh Kumar10/21/01 23:45
Creating thumbnails from mpeg filesRahul10/19/01 03:04
lenght image from and lenght $string ??queru10/17/01 09:28
Generating wbmp via phpMagnus Strømnes Bøe10/11/01 08:44
images with PHPSS Pandav10/10/01 22:01
RE: gd configurationJason Reid10/08/01 21:45
RE: gd configurationIan09/30/01 16:21
GD not initializingjohn09/29/01 14:08
Tough one: placing bitmap text on an imageCurt09/26/01 17:55
RE: function similar to photoshop's FILL rmd09/14/01 22:51
RE: function similar to photoshop's FILL jaydj09/13/01 01:04
RE: About GD libraryKudsy09/09/01 22:50
It is a big article! ..blanks lines .....Marcelo08/23/01 21:03
What is neede to create dynamic image?Baskar08/20/01 08:49
About GD libraryGabriela Dudnik08/19/01 15:48
Images within ImagesTim Wilkes08/17/01 05:58
GIFs in PHP 4.0.6Bard08/01/01 01:15
creating PNG from TIFF or EPS?Ricky Hau07/31/01 02:19
GD Library for PHP 4.05Michael07/27/01 10:32
php programming & the adv.in IT.......kashif ijaz07/19/01 04:14
gd configurationnadenka07/16/01 06:06
How to use ImageTFFText on Win32-Apache ? Chris07/09/01 13:38
Drawing a dynamic line on an base gif sylesh07/04/01 22:02
RE: Acquiring FontsBrian07/03/01 15:33
php imagesron litzenberger06/24/01 11:46
How to put text in center a boxntluan06/10/01 19:01
received php filewilhelmus Neggers06/02/01 07:17
php + gd2.01 = 24-bit colourPhillip Temple05/31/01 10:11
Combine 3 images into oneJesse L. Charbneau05/28/01 13:57
RE: I get an "empty" .gifEarthrise05/18/01 17:29
RE: I get an "empty" .gifMark Hensler05/14/01 22:27
RE: I get an "empty" .gifEarthrise05/10/01 20:52
RE: PHP and GD support/multi-line text imagesNeil P Davis05/07/01 17:30
RE: What is the benefit?Neil P Davis05/07/01 17:22
RE: Colors [STILL]seem greyed out...robert05/02/01 06:41
RE: ImageCreatefromJPEG, Help!Paul04/24/01 15:48
I get an "empty" .gifchristian04/17/01 10:54
RE: Edit an existing imageCharly Pache04/16/01 20:57
EXAMPLE :nice fractal images... c.Kleinhuis04/11/01 09:11
RE: How to Install GDLibrary with jpeg-6bveggie2u04/07/01 17:45
RE: How to Install GDLibrary with jpeg-6bprincess04/06/01 05:42
ImageCreatefromJPEG, Help!David Waldron04/05/01 08:37
RE: Not caching dynamic images7stud04/05/01 00:45
How to Install GDLibrary with jpeg-6bJungRyul Lee03/28/01 11:41
outputting data with no headersEric Blade03/26/01 12:16
RE: Not caching dynamic imagesSridhar Balasubramanian03/23/01 23:33
Not caching dynamic imagesFrederick Wells03/22/01 17:52
cache dynamic images on clientJohn Scrivener03/10/01 11:28
RE: Dynamic Image Creation from a fileskeletor02/09/01 15:19
Get the Binary Datawolfixx02/08/01 06:15
RE: Rasmus or anyone...remi02/06/01 06:34
RE: dynamic cornersMike01/29/01 19:39
What is the benefit?James01/25/01 04:15
RE: how to create thumbnail from larger .jpg?Thorsten Jokiel01/22/01 10:19
how to create thumbnail from larger .jpg?Sean01/17/01 17:00
Edit an existing imageDom01/11/01 22:19
code (long!) RE: dynamic corners rad01/10/01 07:29
RE: Dynamic Image Creation from a filegabi01/10/01 06:09
RE: dynamic cornersDzool01/09/01 14:45
dynamic cornersrad01/09/01 10:25
Dynamic Image Creation from a fileMohan01/06/01 07:22
RE: Rasmus or anyone...Jim01/02/01 13:05
Colors seem greyed out...Andy12/26/00 21:38
Rasmus or anyone...Jim12/21/00 17:05
RE: Writing an imageHuang Wenhai12/19/00 21:06
Doesn't workGlen Scott12/19/00 10:23
PHP + GD + Chinese fontsatan12/19/00 07:00
put image manipulation in html tagYulianto12/15/00 02:49
server load ?Stef12/03/00 01:58
Writing an imageRichard11/29/00 05:25
Addition to previous postingMarc Reich11/26/00 16:10
Working PNG-exampleMarc Reich11/26/00 16:05
PHP and GD support/multi-line text imagesmike11/21/00 11:04
RE: rotate textRegal11/19/00 20:55
image manipulationsandeep 10/27/00 04:17
RE: alignment of image created by this scriptYaxin Wen10/09/00 10:33
alignment of image created by this scriptHarry Roolaart09/16/00 14:34
Its not workingAthaur Rahman09/12/00 02:53
rotate textDan08/08/00 10:44
It need GD library! But I can not use it!HELPTing.Xu07/14/00 18:19
RE: Acquiring FontsRick Morris07/10/00 17:16
Acquiring FontsKevin07/05/00 13:13
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.