Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

GD graphs and CGI

by Anonymous Monk
on Jan 19, 2005 at 13:26 UTC ( #423363=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I'm having problems printing graphs to a web-page from my cgi script. I'm creating the graphs with GD::Graph but when they are displayed on the web I get a load of gibberish like this:

KH*՚@fr'HxA&%4ڃp!7)*R9a
Can anyone tell me whats wrong with the script? The graph is created and printed to file.gif, but is not displayed on my cgi webpage.
Thanks!

@data = ( ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"], [ 1, 2, 5, 6, 3, 1.5, 1, 3, 4] ); + my $graph = GD::Graph::points->new(400, 300); + $graph->set( x_label => 'X Label', y_label => 'Y label', title => 'Some simple graph', y_max_value => 8, y_tick_number => 8, y_label_skip => 2 ) or die $my_graph->error; + + my $gd = $graph->plot(\@data) or die $my_graph->error; open(IMG, '>file.gif') or die $!; binmode IMG; print IMG $gd->png; close IMG; binmode STDOUT; print $gd->png;

Replies are listed 'Best First'.
Re: GD graphs and CGI
by borisz (Canon) on Jan 19, 2005 at 13:32 UTC
    Look s like your content-type is wrong.
    Add content-type: image/png to your headers.
    Boris
Re: GD graphs and CGI
by friedo (Prior) on Jan 19, 2005 at 15:13 UTC
    You are saving your PNG both to an image file (file.gif, which has the wrong extension, it should be file.png) and printing it to STDOUT, where it gets sent to the browser.

    If you want to send your image directly to the browser, then you'll need to send it to STDOUT with a proper content-type header:

    print "Content-type: image/png\n\n"; print $gd->png;

    If you want to save the image on your server and tell the browser to display it, then use:

    open IMG, '>file.png'; print IMG $gd->png; close IMG; print "Content-type: text/html\n\n"; print '<img src="http://yourserver.com/file.png">';

    UpdateFixed typo in content-type.

Re: GD graphs and CGI
by r34d0nl1 (Pilgrim) on Jan 20, 2005 at 12:53 UTC
    It is also important to change you output type to
    # make sure we are writing to a binary stream binmode STDOUT;
    So, when sending the correct header to the browser it will look like an image.
    I had the same problem when dealing with images for the web. I found easier to put the script that generates
    the graphic in a diferent page, staying like this
    <img src='/perl/mygraph.pl'>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://423363]
Approved by skx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2019-12-16 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?