Graphing with PHP using GD - error !

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Graphing with PHP using GD - error !

Post by coool »

hey guys,

I'm having problem when I'm excuting this code

Code: Select all

<?php
        header ("Content-type: image/jpg");
        $img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
        $txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
        ImageString ($img_handle, 31, 5, 5,  "My first Program with GD", $txt_color);
        ImagePng ($img_handle);
?>
This is the output:

Code: Select all

‰PNG  IHDRæ£þÓêPLTE ér¿`ŽLÚIDAT(‘åбAàŸMhF®!ža’+(„WÙË&ª;‘x
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Your displaying a PNG file as a JPEG file. Change your header.
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

I fixed that.. but I've got same result :(

Code: Select all

<?php
        header ("Content-type: image/png");
        $img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
        $txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
        ImageString ($img_handle, 31, 5, 5,  "My first Program with GD", $txt_color);
        ImagePng ($img_handle);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you calling this code in the middle of a page?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You'll find it useful to debug image development if you change things around a little and add a toggle.. eg:

Code: Select all

<?php

        $debug = true;

        if ($debug==true) {
                error_reporting(E_ALL);
                ini_set("error_reporting",TRUE);
                ini_set("display_errors", "on");
        }

        $img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
        $txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
        ImageString ($img_handle, 31, 5, 5,  "My first Program with GD", $txt_color);

        if ($debug!=true) {
                header ("Content-type: image/png");
                ImagePng ($img_handle);
        }
?>
Now if you call the image script it'll output any errors like a normal script.
tapas_bahirbag
Forum Newbie
Posts: 14
Joined: Sun Aug 06, 2006 6:54 am
Contact:

Post by tapas_bahirbag »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php
        header ("Content-type: image/png");
        $img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
        $txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
        ImageString ($img_handle, 31, 5, 5,  "My first Program with GD", $txt_color);
        ImagePng ($img_handle);
?>
This is your code and this is working properly in my localhost.

--
Tapos Pal


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

onion2k wrote:You'll find it useful to debug image development if you change things around a little and add a toggle.. eg:

Code: Select all

<?php

        $debug = true;

        if ($debug==true) {
                error_reporting(E_ALL);
                ini_set("error_reporting",TRUE);
                ini_set("display_errors", "on");
        }

        $img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
        $txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
        ImageString ($img_handle, 31, 5, 5,  "My first Program with GD", $txt_color);

        if ($debug!=true) {
                header ("Content-type: image/png");
                ImagePng ($img_handle);
        }
?>
Now if you call the image script it'll output any errors like a normal script.
I've copied and past the above code ..

it shows nothing :( ... no error and nothing !!
purinkle
Forum Newbie
Posts: 12
Joined: Fri Jul 13, 2007 6:54 am

Post by purinkle »

Are you calling the code inside another PHP script or is the code in a separate file of it's own?
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

feyd wrote:Are you calling this code in the middle of a page?

yes ! ... but I've tried it without being in the middle of the page.. it's says:

Warning: Cannot modify header information - headers already sent by (output started at /localhost/test_graph.php:17) in /localhost/test_graph.php on line 18

‰PNG IHDRæ£þÓêPLTE ér¿`ŽLÚIDAT(‘åбAàŸMhF®!ža’+(„WÙË&ª;‘x
purinkle
Forum Newbie
Posts: 12
Joined: Fri Jul 13, 2007 6:54 am

Post by purinkle »

I tried it with the debug stuff and it didn't work

Without the debug stuff I got:
Image

Are you sure you have the GD Libraries installed?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

purinkle wrote:I tried it with the debug stuff and it didn't work
By "it doesn't work" do you mean "it doesn't output an image"? Because it's not supposed to.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[quote="coool"]yes ! ... but I've tried it without being in the middle of the page.. it's says:

Warning: Cannot modify header information - headers already sent by (output started at /localhost/test_graph.php:17) in /localhost/test_graph.php on line 18

‰PNG IHDRæ£þÓêPLTE ér¿`ŽLÚIDAT(‘åбAàŸMhF®!ža’+(„WÙË&ª;‘x
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

can you please show me a little example about what you're saying?

just a little one :)

thanks in advance..
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

coool wrote:can you please show me a little example about what you're saying?
This code will not work:

page.php ...

Code: Select all

<html>
<head></head>
<body>
<?php 
    $image = imagecreate(200,200);
    header("Content-type: image/png");
    imagepng($image);
?>
</body>
</html>
You have to use two separate scripts:

page.php ...

Code: Select all

<html>
<head></head>
<body>
<img src="image.php">
</body>
</html>
image.php ...

Code: Select all

<?php 
    $image = imagecreate(200,200);
    header("Content-type: image/png");
    imagepng($image);
?>
The img tag in page.php loads the image generated by image.php.
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

WOW .. it's working :)

I'm happpyyy.. thaaaanks very much

okay ! .. now how can have a graph ! with x-axis and y-axis taken from one table in MySQL query !

for example:

FruitsTable
fruitName numberAvailable
Apple 2
Orange 8
Banana 5

I want the x-axis to be my fruitName
and the y-axis to be the numberAvailable of the fruit

Query = "SELECT fruitName, numberAvailable FROM FruitsTable"

then ?
Post Reply