/    Sign up×
Community /Pin to ProfileBookmark

Drawing Bar Graphs using GD/MySQL

Hi..

I’m having problem in this code !

I beileve it’s almost done, but just need a little fix !

here’s the code:
[B]image.php [/B]

[code=php]
<?php
//already been connected to database

$sql = “SELECT Status FROM Table1 GROUP BY Status”;
$x = mysql_query($sql) or die(“Error – Could not $sql” . mysql_error());

$sql = “SELECT COUNT(Items) FROM Table1 GROUP BY Status”;
$y = mysql_query($sql) or die(“Error – Could not $sql” . mysql_error());

//x= sold,shipped,InStock,UnKnown
//y= 8,6,3,5

$width = 300;
$height = 200;

header (“Content-type: image/png”);

$image = ImageCreate($width,$height) or die (“Cannot Create image”);

$white = ImageColorAllocate($image,255,255,255);
$black = ImageColorAllocate($image,0,0,0);
$red = ImageColorAllocate($image,255,0,0);
$green = ImageColorAllocate($image,0,255,0);
$blue = ImageColorAllocate($image,0,0,255);

$maxY = 0;
for ($i=0;$i<7;$i++){
if ($y[$i] > $maxY)$maxY=$y[$i];
}

ImageRectangle($image,1,1,319,239,$black);

imageLine($image,10,5,10,230,$blue);
imageLine($image,10,230,300,230,$blue);

ImageString($im,3,15,5,”CR ID”,$black);
ImageString($im,3,280,240,”Status”,$black);
ImageString($im,5,100,50,”Simple Graph”,$red);
ImageString($im,5,125,75,”by Lina”,$green);

$xPos = 15;
$yPos = 230;
$barWidth = 20;
$barHeight = 0;

for ($i=0;$i<3;$i++){
$barHeight = ($y[$i]/$maxY)* 100;
imagerectangle($image,$xPos,$yPos,$xPos+$barWidth,($yPos-$barHeight),$red);
imagestring( $image,2,$xPos-1,$yPos+1,$x[$i],$black);
imagestring( $image,2,$xPos-1,$yPos+10,$y[$i],$black);
$xPos += ($barWidth+20);
}

ImagePng($image, “graph.png”, 90);
?>
[/code]

[B]page.php[/B]

[code]
….
<img src=”image.php”/>
….
[/code]

[B]Error:[/B] after excuting page.php
The image “[url]http://websiteName.com/image.php”[/url] cannot be displayed, because it contains errors.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Kostas_ZotosJul 30.2007 — Hi,

From a first look, your code seems OK to me, except from the image output (one line from the end).. I don't know the version of your PHP but the "imagepng" i think, can get maximum 2 arguments, the image object and optionally a file name (string) if you want to save on disk rather to output directly to browser. The compressed (quality) argument not applied in the imagepng function..

In conclusion replace the [B]ImagePng($image, "graph.png", 90); [/B]

with: [B]ImagePng($image);[/B]

Kostas
Copy linkTweet thisAlerts:
@Kostas_ZotosJul 31.2007 — Regarding the arguments, sorry, seems that I am a bit back...

The synatx of "imagepng" is as follow (from the official site):

[B]bool imagepng ( resource $image [, string $filename [, int $quality [, int $filters]]] )[/B]

Just the quality must ranges from: 0 to 9

Then if you want to use the quality you may modify your command like this:

[B]ImagePng($image, null, 8);[/B] // or quality 9 etc..

Sorry for the incorrect info given in my previous post ?
Copy linkTweet thisAlerts:
@MrCoderJul 31.2007 — The image “http://websiteName.com/image.php” cannot be displayed, because it contains errors.[/QUOTE]

View the page source in your browser, there will be a PHP error.
Copy linkTweet thisAlerts:
@Kostas_ZotosAug 01.2007 — I took a closer look to your code and finally before start to work returned 2 more issues:1) a "for" loop which was trying to read beyond $y array's length.. and 2) a $im variable name i assume instead of $image..

After these 2 justifications it works for me (note: I changed also a bit the image dimensions as some parts appeared partially cropped)

This is the code (your code with the above modifications):
[code=php]<?php
//already been connected to database

$sql = "SELECT Status FROM Table1 GROUP BY Status";
$x = mysql_query($sql) or die("Error - Could not $sql" . mysql_error());

$sql = "SELECT COUNT(Items) FROM Table1 GROUP BY Status";
$y = mysql_query($sql) or die("Error - Could not $sql" . mysql_error());

//x= sold,shipped,InStock,UnKnown
//y= 8,6,3,5

$width = 340;
$height = 265;

$image = ImageCreate($width,$height) or die ("Cannot Create image");

$white = ImageColorAllocate($image,255,255,255);
$black = ImageColorAllocate($image,0,0,0);
$red = ImageColorAllocate($image,255,0,0);
$green = ImageColorAllocate($image,0,255,0);
$blue = ImageColorAllocate($image,0,0,255);

$maxY = 0;
for ($i=0;$i<count($y);$i++){ // count($y) returns the total number of elements on the given array
if ($y[$i] > $maxY) $maxY=$y[$i];
}

ImageRectangle($image,0,0, $width-1, $height-1, $black);

imageLine($image,10,5,10,230,$blue);
imageLine($image,10,230,300,230,$blue);

ImageString($image,3,15,5,"CR ID",$black);
ImageString($image,3,280,240,"Status",$black);
ImageString($image,5,100,50,"Simple Graph",$red);
ImageString($image,5,125,75,"by Lina",$green);

$xPos = 15;
$yPos = 230;
$barWidth = 20;
$barHeight = 0;

for ($i=0;$i<3;$i++){
$barHeight = ($y[$i]/$maxY)* 100;
imagerectangle($image,$xPos,$yPos,$xPos+$barWidth,($yPos-$barHeight),$red);
imagestring( $image,2,$xPos-1,$yPos+1,$x[$i],$black);
imagestring( $image,2,$xPos-1,$yPos+14,$y[$i],$black);
$xPos +=( $barWidth+25);
}

header ('Content-type: image/png');
ImagePng($image, null , 2); // 2=> Level of compression 0-9 (lower values => better quality -larger file- )
// Use "null" as file name to get output directly to browser
// Or a valid file name to get the image only as external "saved file" in the disk. (in this case you will not get an output to browser)
?>[/code]


Kostas
Copy linkTweet thisAlerts:
@cooolauthorAug 03.2007 — I really thank very much for your help

things worked perfectly with

ImagePng($image, NULL, 2);

and with

ImagePng($image);

?
×

Success!

Help @coool spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...