Jump to content

Graphing with PHP using GD - error !


coool

Recommended Posts

hey guys,

 

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

 

<?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:

 

‰PNG  IHDRæ£þÓêPLTE ér¿`ŽLÚIDAT(‘åбAàŸMhF®!ža’+(„WÙË&ª;‘xë”Z…‡Ði'J­D£RÑ+®0wÁ¯`«ÙýòÏfø³Sc CYeŸGEý˜õà«*T,(´ªšðñ£Peí|&H»;?&¦±éÖ³½54IÇ%¥öÉé¿×‘[¢•!‘i¡)1kg$;ÇP E‚˜o™¶²,{»—z÷·†…Âå:`Ž¾´¹Ì”¨Ž,³fSñƒ8Ÿ¨Æ7ȽíEµ¯E´ª,ÒNçÛ@µ4&‚øg••Gé2ü¶'€£D?Ñ<_FIEND®B`‚ 

 

what's wrong with my code !

 

do you have any clue ?

Link to comment
Share on other sites

<?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);
?>

 

youre sending the headers as jpg but outputting png.. you either change the header to png or the output to jpg

Link to comment
Share on other sites

 

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

 

<?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);
?>

Link to comment
Share on other sites

This is what's in my phpinfo about GD:

 

gd

GD Support            enabled 

GD Version              bundled (2.0.28 compatible) 

GIF Read Support    enabled 

GIF Create Support  enabled 

PNG Support            enabled 

WBMP Support        enabled 

XBM Support          enabled 

 

I think that means GD is enabled.. right ??

 

Link to comment
Share on other sites

Your code on my server works perfectly.

 

Are you 100% sure you uploaded your revised code to the same server folder as you're viewing to test the script?

 

yes I'm sure..

 

what's ur GD criteria in phpinfo ?

Link to comment
Share on other sites

Problem is solved..

 

I just saparated the codes..

 

image.php

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

 

page.php

<html>

<head></head>

<body>

 

<img src="image.php"/>

 

</body>

</html>

 

 

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 ?

Link to comment
Share on other sites

Oh, well your initial problem is page.php contained data before you were trying to output. In this case, what you did was semi-correct.

 

For performance reasons, you shouldn't have to render the image each time the page is loaded, maybe you should, it depends on what you're trying to achieve.

 

One last thing, if you don't want to use <img src="some.php"> you can output (using the same ImagePNG function) to a file, simply by changing the function call to look like:

 

<?php
        header ("Content-type: image/jpg");
        $image = ImageCreate (230, 20) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($image, 1, 10, 10);
        $text_color = ImageColorAllocate ($image, 233, 114, 191);
        ImageString ($image, 31, 5, 5,  "My first Program with GD", $text_color);
        ImageJPEG ($image, "nameofjpg.jpg");
?>

 

 

Link to comment
Share on other sites

Easy way to graph is to have a simple bar graph

 

::bar.php::

<?php
// set dimensions
     $w = 102;
     $h = 20;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $red  = imagecolorallocate($im, 0xFF, 0x00, 0x00);
       
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
     $val = isset($_GET['val']) ? $_GET['val'] : 0;
     $max = isset($_GET['max']) ? $_GET['max'] : 100; 
// calculate dimensions of inner bar
     $barw = $max ? floor(($w-2) * $val / $max) : 0;
     $barh = $h - 2;
// draw inner bar
      if ($barw)
     {
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $red);
     }
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

 

then

 

<?php
$ar = array(
    'Apple'  => 2,
    'Orange' => 8,
    'Banana' => 5
);

echo '<table>';
foreach ($ar as $fruit => $qty)
{
    echo "<tr><td>$fruit</td><td><img src='bar.php?val=$qty&max=10'></td></tr>";
}
echo '</table>';
?>

Link to comment
Share on other sites

Oh, well your initial problem is page.php contained data before you were trying to output. In this case, what you did was semi-correct.

 

For performance reasons, you shouldn't have to render the image each time the page is loaded, maybe you should, it depends on what you're trying to achieve.

 

One last thing, if you don't want to use <img src="some.php"> you can output (using the same ImagePNG function) to a file, simply by changing the function call to look like:

 

<?php
        header ("Content-type: image/jpg");
        $image = ImageCreate (230, 20) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($image, 1, 10, 10);
        $text_color = ImageColorAllocate ($image, 233, 114, 191);
        ImageString ($image, 31, 5, 5,  "My first Program with GD", $text_color);
        ImageJPEG ($image, "nameofjpg.jpg");
?>

 

 

 

I've tried your code.. it doesn't work !

 

that's okay I'll stay with page.php and image.php

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.