SiteMinder / Webhosting
The Computer Merchant, Ltd
US-MA-North Quincy

Justtechjobs.com Post A Job | Post A Resume

Graphing With PHP and GD
Before we can start drawing the graph, let's start with the basics and look at how PHP creates an image. The first thing that we need to do is tell the browser that it's getting an image, and what kind of image it's getting:

<?php

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

?>
Now that the browser knows it's getting a GIF image we can start creating the image. First up we have to create a blank canvas to start drawing on. The ImageCreate function does this for us. ImageCreate will return an identifier to the image and it we need to tell the function is how large to make the canvas in pixels, x (width) by y (height).

<?php

$image
= imagecreate(200,200);

?>
Now we've got a 200 pixels by 200 pixels blank canvas to start working on. The next step is to create some colors that are going to be used in the image. For this we have to use the ImageColorAllocate function, and it needs to know the identifier of the image that the color is for, as well as the RGB value of the color. ImageColorAllocate will return an identifier to the color that we just created. We will use the color identifier later when we start drawing on the canvas. The way that ImageColorAllocate works is that we have to allocate a color for each image that we are working on - so if we were creating 3 GIF's and wanted red in each of them, we would have to allocate the color red 3 times - once for each GIF. I'll allocate a color identifier called $maroon, and give the red value 100, green 0 and blue 0. While I'm at it I'll create white as well.

<?php

$maroon
= ImageColorAllocate($image,100,0,0);
$white = ImageColorAllocate($image,255,255,255);

?>
Now that we've got our color, we can draw something with it. The first thing to do is paint our canvas white. The function ImageFilledRectangle will draw a rectangle on our canvas and fill it with the color we specify.

<?php

    ImageFilledRectangle
($image,0,0,200,200,$white);

?>
The first thing to tell ImageFilledRectangle, as with all Image functions, is which image we are working with, so we pass it the $image identifier. It then needs to know the x and y co-ordinates to start the rectangle at (0,0 - the upper left hand corner) and the co-ordinates to end the rectangle at (200,200 - the bottom right hand corner of the canvas). The last thing to tell it is the identifier of the color to draw the rectangle in, in this case $white. Now we can start drawing on our nice white background.

<?php

ImageRectangle
($image,10,10,190,190,$maroon);
ImageFilledRectangle($image,50,50,150,150,$maroon);

?>
ImageRectangle works in exactly the same way as ImageFilledRectangle, except that it doesn't fill the rectangle with color. Once we are done drawing, we can output the image -

<?php

ImageGIF
($image);

?>
And then destroy the image that we are storing in memory:

<?php

ImageDestroy
($image);

?>
What this gives us is:
Maroon Square
which is not quite a graph, yet.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
GD 2.0.33joelrf11/22/04 11:41
interestingjesse estner10/27/03 07:44
The foundations are not mentioned...David Van Brunt07/03/03 14:19
Header InfoNeubie01/31/03 08:13
RE: GD + HTMLBossTI8901/30/03 09:02
Bad table structureJoonas01/17/03 03:30
RE: warning - cannot add header information..Greg Terry01/07/03 10:37
RE: PHP GD. DDL for WindowsCarlos E.11/08/02 02:45
RE: New to php Robear11/01/02 14:05
New to php me and me 10/11/02 17:24
How to create svg from xml and xsl with php?tahereh10/08/02 17:15
RE: Axis and labelssindhoor10/08/02 12:07
commentfred09/26/02 10:09
RE: Help with PHPPR63709/16/02 14:20
Help with PHPGunnerX09/13/02 13:02
how to complie php for graphicsSRIKANT09/07/02 12:07
RE: No GIF Support in this PHP build..kev08/26/02 13:20
RE: Call to undefined function: imagecreate()SivaSankari08/23/02 01:39
how come like thishong lipp ko08/02/02 13:19
how come like thishong lipp ko08/02/02 13:18
Call to undefined function: imagecreate()Suzan08/01/02 08:00
No GIF Support in this PHP build..Arnout Veenman07/31/02 08:44
RE: Support for png and Jpeg on gdfilippo07/31/02 08:29
Support for png and Jpeg on gdLeandro07/18/02 09:58
problem with phplot4.4.6 + Apache +Win2kPhuc Trong07/16/02 22:34
RE: GD + HTMLSmurf! 07/16/02 10:10
Entry Point Not Found Error.Josh Suggs07/02/02 01:19
Fatal error: Cannot instantiate non-existent Kekeletso Lengoasa06/05/02 06:49
RE: Problems with gd. Help please.Manuel05/31/02 12:42
RE: Where to get GD for Windows -Apache-PHP InduB05/31/02 09:51
How can I read the answers for all these quesJaro Piipari05/29/02 05:15
RE: GD working with Netscape not with IE5YoPi05/26/02 09:36
Where to get GD for Windows -Apache-PHP Rabbani05/20/02 01:32
main page - capture»æ¼ö05/16/02 01:31
graphing helpShreya05/15/02 10:55
Howto get PHP_GD.DLL for Windowsdeni05/12/02 21:54
how to install gd for PHP in Win2kAShish Gupta05/07/02 11:54
GD working with Netscape not with IE5Pravish K Viswanathan05/03/02 06:55
Blank Images...brian04/24/02 11:54
Copying images problem in php 4.1.2Jack04/22/02 07:39
GD installed w/o PNG, JPEGJohn Kolvereid04/14/02 11:52
RE: gd install helpClemenca04/13/02 15:51
error: imagegif() cannot create fileluke04/13/02 12:45
RE: GD + HTMLMichael Connors04/11/02 12:30
php GD and perl GDsambaiah04/11/02 04:01
RE: I need help in PHPAcoyauh04/01/02 13:50
GIF in GDAmin KUMIZ UKDW03/28/02 12:59
I need help in PHPMaged03/28/02 01:14
RE: warning - cannot add header information..Glenn Barres03/12/02 12:16
RFC: GD build problems in RH 7.2m.strijbos03/12/02 08:26
I need help for you! RH7.2+PHP4.12+GDmayj03/10/02 22:38
gd install helpYour name here03/10/02 01:45
RE: GD and PHP4.1.2Dave Hale03/07/02 04:04
RE: GD and PHP4.1.2Mike Siekkinen03/05/02 21:25
GD and PHP4.1.2Joe Cho02/28/02 22:32
PHP SCRIPTMAHA02/18/02 05:16
RE: If your having problems with GD read this!!Alex02/13/02 08:43
Creating graphs in PHPjayakrishnsn02/04/02 01:45
Call to undefined function imagecreate() Szymon02/03/02 06:04
How to use php-4.0.6RC2.tar.gz?Ahfang01/29/02 00:16
If your having problems with GD read this!!Leonard01/26/02 21:51
No TTF support in this PHP build Christian O.01/18/02 16:28
GD + HTMLDiego Escalante01/17/02 22:14
RE: Problems with gd. Help please.Diego Escalante01/16/02 20:38
RE: GD + PHP + Windows (Solve)Diego Escalante01/16/02 20:36
How do you get the gd to work on window?sulawan01/15/02 04:29
To get gifs working...Adam01/09/02 21:54
php_gd.dllHorhe12/27/01 07:09
How can i install GD with php? at linuxBataa12/05/01 03:55
RE: warning - cannot add header information..Kawagator12/04/01 09:31
GD+PHP 4.0.6 compile error, conflicting typespolo11/20/01 22:15
RE: GD + PHP + Windows (Solve)Marquese Howard11/18/01 09:03
RE: scripts output bmps instead of jpegs...Malte11/12/01 10:35
how to use TTF woth libttf.dllRonald Nooy11/09/01 19:16
RE: Howto get PHP_GD.DLL for WindowsSteve Belanger11/06/01 16:28
RE: Call to undefined function: imagecreate() Irondog11/05/01 07:24
gd + PHP Helpimran10/23/01 03:08
MESSAGE No GIF support in this PHP build instefano10/19/01 08:56
Resizing GIF/JPGShezar10/17/01 12:25
gd 1-3 and gd 1-8Charles Gartrell10/15/01 21:31
RE: Howto get PHP_GD.DLL for Windowsyselim09/20/01 11:21
scripts output bmps instead of jpegs...tekin09/14/01 08:02
RE: using imagesJens P. Hansen09/05/01 01:16
RE: another problem with GDsean08/17/01 04:04
RE: another problem with GDDave Appleton08/09/01 11:23
RE: another problem with GDErhard08/03/01 03:45
RE: jpgraphMichal07/27/01 09:37
How do you get the GD to work?Michal07/24/01 14:41
GD, PHP with Win2k problemsPatrick Simon07/19/01 04:28
warning - cannot add header information..Hal07/13/01 02:44
RE: jpgraphJoseph L.07/12/01 12:34
RE: another problem with GDarmzy07/06/01 08:29
RE: help - imagesarmzy07/06/01 08:22
Call to undefined function: imagecreate() christophe07/02/01 14:08
another problem with GDTads06/18/01 06:57
help - imagesTads06/12/01 08:30
GD+PHP 4.0.5 compile error, conflicting typesJonas Rosenqvist05/25/01 04:04
jpgraphCameron Green04/26/01 02:22
RE: problem with gdmodulus04/18/01 17:25
I cant use gd.dllyona03/25/01 08:08
RE: Dynamically put a border on an imageShamim03/21/01 04:13
Dynamically put a border on an imageCharles03/16/01 01:10
RE: Howto get PHP_GD.DLL for WindowsMJJuan03/07/01 10:52
Freetype 2.0+ Support in PHP (even CVS) ?Justin Heesemann02/26/01 13:47
using imagesDan Allen02/25/01 04:28
PHP SNMPNolberto A. Devia Espinosa02/12/01 07:47
mod_php4.0.4pl1 and gd 1.3Reto Burkhalter02/05/01 07:47
I need a compilled version for LINUXBert01/07/01 12:26
RE: RE: Graphing in 3 dimensional spaceEdgard Berendsen01/07/01 11:18
RE: RE: Graphing in 3 dimensional spacePatrick Mineault01/06/01 05:36
SCO UNIX 5 + PHP4 + GD , please helpZXE12/24/00 02:57
RE: problem with gdyudi12/17/00 22:30
File name for the imageMax12/14/00 07:53
wrapperextension Muhammed 12/11/00 08:10
RE: Howto get PHP_GD.DLL for WindowsCarl G. Looney12/10/00 19:08
Axis and labelsGeo12/07/00 15:19
Sending text and graphsBrian Carlson12/03/00 14:05
Merge 4 Pic incl. Masks and antialaising Rene11/22/00 09:04
RE: Problems with gd. in IIS5Joe Cool11/15/00 05:05
RE: problems with gd in phphighend11/15/00 05:01
RE: Merging two imagesHerman Veluwenkamp11/13/00 15:46
problems with gd in phpgarvin casimir11/08/00 19:25
I Cant use gd on RH7.0 :[Aril11/04/00 04:34
GD + PHP4 , please helpIrwan 10/31/00 05:22
RE: Problems with gd. Help please.Gill Bates10/20/00 15:39
Merging two imagesLeonardo Pabon10/08/00 13:22
Problems with gd. Help please.Fernando10/05/00 17:17
RE: Howto get PHP_GD.DLL for Windowsdanny w09/29/00 01:41
Any ideas how to do it for small numbersArkadi09/16/00 04:26
RE: apache: gd functions multiply defined!Rick Morris09/14/00 00:57
Graphing With PHP and GDAndreas Czechowski09/13/00 09:57
apache: gd functions multiply defined!Jim Hood09/08/00 13:45
RE: 3d Graphs and VRMLAlex V09/06/00 01:22
RE: Howto get PHP_GD.DLL for WindowsTony Stapells09/05/00 21:20
3d Graphs and VRMLMichael09/05/00 16:31
Howto get PHP_GD.DLL for WindowsJohn Lim09/05/00 10:31
Stripping certain values submitted...Koray Peksayar09/04/00 16:12
RE: Old Windows GD <1.6 binaries?Allan Kent09/04/00 12:39
Old Windows GD <1.6 binaries?Tony Stapells09/03/00 21:34
Check out VHGraphLeonard Linde09/03/00 11:39
RE: GD And Zindo$$Gill Bates09/02/00 16:23
RE: Graphing in 3 dimensional spaceKartic Krishnamurthy09/01/00 20:00
Graphing in 3 dimensional spaceMarek Dlugozima09/01/00 14:22
RE: GD And Zindo$$Allan Kent09/01/00 08:26
GD And Zindo$$Shinobi09/01/00 06:22
GIF not in latest GD libraryVincent08/31/00 07:34
 

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.