• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2560
  • Last Modified:

PHP5 GD (JpGraph) Garbled Text (binary output in web browser)

I'm trying to get JpGraph up and running with the GD library and PHP but when I use the below script from the JpGraph documentation "Verifying that you have the GD library installed" -- all I get is garbled text where the image should be like this, "‰PNG  IHDR,ÈݽK¢IDATxœíÜ1....".  I don't get any errors, just garbled text.  How do I get an image instead of binary output on my browser window?

<?php
 $im = @ ImageCreate ( 150, 100)
    or die ( "Cannot create a new GD image.");
$background_color  = ImageColorAllocate  ($im, 255,  255, 255);
$text_color  = ImageColorAllocate  ($im, 233,  14, 91);
ImageString  ($im, 1,  5, 5,  "A Simple Text String", $text_color );
header ("Content-type: image/png");
ImagePng ($im);
?>

gd_info()
GD Support  enabled  
GD Version  bundled (2.0.28 compatible)  
FreeType Support  enabled  
FreeType Linkage  with freetype  
FreeType Version  2.1.9  
GIF Read Support  enabled  
GIF Create Support  enabled  
JPG Support  enabled  
PNG Support  enabled  
WBMP Support  enabled  
XBM Support  enabled  
Server API  Apache 2.0 Handler  
System  Windows NT 5.0 build 2195  
Registered PHP Streams  php, file, http, ftp, compress.zlib  
PHP Version 5.0.4RC2-dev
http://www.t1shopper.com/
0
Geoff Millikan
Asked:
Geoff Millikan
  • 4
  • 2
1 Solution
 
Geoff MillikanAuthor Commented:
I updated to "PHP Version 5.2.0-dev" and Apache 2.2.2 to see if that would fix anything and it didn't make it better.  phpinfo says:
Configure Command  cscript /nologo configure.js "--enable-snapshot-build" "--with-gd=shared"
_SERVER["SERVER_SOFTWARE"] Apache/2.2.2 (Win32) DAV/2 PHP/5.2.0-dev

Help!
0
 
Geoff MillikanAuthor Commented:
I've tried everything and now I'm back to Apache/2.0.52 (Win32) PHP/5.2.0-dev and the image is still coming out as gibberish binary data instead of an image.  I give up.  Help!
0
 
Richard QuadlingSenior Software DeveloperCommented:
I suspect that there is SOMETHING happening to change the header you are sending.

Now, according to the manual, you do NOT need to use the header().

<?php
$im = imagecreatefrompng("test.png");
imagepng($im);
?>


But there is a user note : vladson at pc-labs dot info (04-Jul-2005 04:00)

To send 'Content-Length' header (like in static pictures case) i use "Output handler" like this...
<?php
// Output handler
function output_handler($img) {
header('Content-type: image/png');
header('Content-Length: ' . strlen($img));
return $img;
}
// Image output
ob_start("output_handler");
imagepng($im);
ob_end_flush();
?>


So ...

1 - Try removing the header('Content-type: image/png'); line
2 - Try using the output_handler example code above.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Geoff MillikanAuthor Commented:
Running the above smaple script #1 provided by RQuadling produced:
Warning: imagecreatefrompng(test.png) [function.imagecreatefrompng.html]: failed to open stream: No such file or directory in C:\Web\dev\gd.php on line 11
Warning: imagepng(): supplied argument is not a valid Image resource in C:\Web\dev\gd.php on line 12

Running the above sample script #2 provided by RQuadling produced:
Warning: imagepng(): supplied argument is not a valid Image resource in C:\Web\dev\gd.php on line 24

Help!
0
 
Geoff MillikanAuthor Commented:
Although this problem was not resolved, thanks so much for your help - I'm going to award you points anyway for the effort.  This charting software was too hard to figure out so we went with another solution http://www.infosoftglobal.com/FusionCharts/
0
 
Richard QuadlingSenior Software DeveloperCommented:
Ah!

Come on, did you not actually READ the code?

imagecreatefrompng(...)

means there has to be an existing PNG file.

The issue is your additional header statement.

Did you try just removing it?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now