coool Posted July 12, 2007 Share Posted July 12, 2007 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'JD£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 ? Quote Link to comment Share on other sites More sharing options...
Rojay Posted July 12, 2007 Share Posted July 12, 2007 <?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 Quote Link to comment Share on other sites More sharing options...
coool Posted July 12, 2007 Author Share Posted July 12, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 12, 2007 Share Posted July 12, 2007 There's nothing wrong with that code. It works perfectly uploaded to my GD-enabled server. Does your server support GD? Quote Link to comment Share on other sites More sharing options...
jchemie Posted July 13, 2007 Share Posted July 13, 2007 Hey you need to graph? If its for non commercial use, i would seriouly recommend using the jpgraph lib. Check it out at http://www.aditus.nu/jpgraph/ Thanks Jyot Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 13, 2007 Share Posted July 13, 2007 If you call phpinfo(); you can scroll down the output and check if GD is enabled. Quote Link to comment Share on other sites More sharing options...
coool Posted July 13, 2007 Author Share Posted July 13, 2007 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 ?? Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 13, 2007 Share Posted July 13, 2007 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? Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 13, 2007 Share Posted July 13, 2007 Works just fine for me. Quote Link to comment Share on other sites More sharing options...
coool Posted July 16, 2007 Author Share Posted July 16, 2007 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 ? Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 Png doesnt work with IE natively. Are you using IE? Quote Link to comment Share on other sites More sharing options...
coool Posted July 16, 2007 Author Share Posted July 16, 2007 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 ? Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 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"); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 16, 2007 Share Posted July 16, 2007 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>'; ?> Quote Link to comment Share on other sites More sharing options...
keeB Posted July 17, 2007 Share Posted July 17, 2007 Great example, Barand! Quote Link to comment Share on other sites More sharing options...
coool Posted July 17, 2007 Author Share Posted July 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
coool Posted July 17, 2007 Author Share Posted July 17, 2007 Thanks Barand -- that's really a nice, simple, clear example Quote Link to comment Share on other sites More sharing options...
Barand Posted July 17, 2007 Share Posted July 17, 2007 [ I've tried your code.. it doesn't work ! Mime type should be "image/jpeg" header ("Content-type: image/jpeg"); Quote Link to comment Share on other sites More sharing options...
coool Posted July 27, 2007 Author Share Posted July 27, 2007 Thanks I like the example ! but there're some things missing ! what i'm looking for is something like the attached picture please take a look [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.