Page 1 of 2

Graphing with PHP using GD - error !

Posted: Thu Jul 12, 2007 2:39 pm
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

Posted: Thu Jul 12, 2007 4:02 pm
by Ambush Commander
Your displaying a PNG file as a JPEG file. Change your header.

Posted: Thu Jul 12, 2007 9:11 pm
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);
?>

Posted: Thu Jul 12, 2007 9:36 pm
by feyd
Are you calling this code in the middle of a page?

Posted: Fri Jul 13, 2007 3:15 am
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.

Posted: Fri Jul 13, 2007 4:54 am
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]

Posted: Fri Jul 13, 2007 9:16 am
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 !!

Posted: Fri Jul 13, 2007 9:19 am
by purinkle
Are you calling the code inside another PHP script or is the code in a separate file of it's own?

Posted: Fri Jul 13, 2007 9:22 am
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

Posted: Fri Jul 13, 2007 9:31 am
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?

Posted: Fri Jul 13, 2007 11:00 am
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.

Posted: Fri Jul 13, 2007 11:23 pm
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

Posted: Sun Jul 15, 2007 11:48 pm
by coool
can you please show me a little example about what you're saying?

just a little one :)

thanks in advance..

Posted: Mon Jul 16, 2007 4:00 am
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.

Posted: Mon Jul 16, 2007 9:23 am
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 ?