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

Posted on 2006-05-30
Last Modified: 2013-12-13
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?

 $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 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
Question by:Geoff Millikan

    Author Comment

    by:Geoff Millikan
    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


    Author Comment

    by:Geoff Millikan
    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!
    LVL 40

    Accepted Solution

    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().

    $im = imagecreatefrompng("test.png");

    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...
    // Output handler
    function output_handler($img) {
    header('Content-type: image/png');
    header('Content-Length: ' . strlen($img));
    return $img;
    // Image output

    So ...

    1 - Try removing the header('Content-type: image/png'); line
    2 - Try using the output_handler example code above.

    Author Comment

    by:Geoff Millikan
    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


    Author Comment

    by:Geoff Millikan
    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
    LVL 40

    Expert Comment


    Come on, did you not actually READ the code?


    means there has to be an existing PNG file.

    The issue is your additional header statement.

    Did you try just removing it?

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    27 Experts available now in Live!

    Get 1:1 Help Now