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

This is my first try with GD::Graph. I'd like to 'print' the image directly to the browser without having to save it via a filehandle. Alas it is not yet working. What have I missed?

<%perl> use GD::Graph::bars; use Data::Dumper; my ($x, $y, @numbers, $number); my @data = ( # X axis [ qw( -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 )] ); # loop for each success level for ($x = 1, $x < 5, $x++){ # generate numbers for each skill level for ($y = 1, $y < 19, $y++){ $number = $x*$y*2+5; push @numbers, $number; } push @data, [@numbers]; } my $graph = new GD::Graph::bars(); $graph->set( title => 'Skill Resolution', x_label => 'Skill Level', y_label => 'Success Level' ); my $gd = $graph->plot(\@data) or die "Could not plot: ".$graph->error. Dumper(@data); print "Content-type: image/png\n\n"; print $gd->png(); </%perl>
Yes, this is done through Mason.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: GD::Graph without saving file
by PodMaster (Abbot) on Jul 07, 2004 at 23:33 UTC
    1. You're forgetting to binmode. 2. That's not how you deal with headers in mason (see Re: OT: HTML::Mason & apache redirects)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      OK so using binmode and the Mason way of headers (all within %init instead of %perl):

      ....snip... my $gd = $graph->plot(\@data) or die "Could not plot: ".$graph->error. Dumper(@data); my $img = $gd->png(); $r->content_type("image/png"); $r->send_http_header; binmode STDOUT; $m->print($img); $m->abort(OK); # make sure nothing else gets sent
      Still not working.

      Neil Watson
      watson-wilson.ca

        Still not working.

        If you comment out the

        $m->print($img);

        line and make the request by hand (e.g., in a telnet session to port 80), what do the headers look like?

        Why? Just binmode, it can't hurt.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: GD::Graph without saving file
by neilwatson (Priest) on Jul 12, 2004 at 13:36 UTC
    Here is the working code:

    <%init> use GD::Graph::bars; use Data::Dumper; my ($x, $y, @numbers, $number); my @data = ( # X axis [ qw( -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 )] ); # loop for each success level for ($x = 1; $x < 5; $x++){ # generate numbers for each skill level for ($y = 1; $y < 19; $y++){ $number = $x*$y*2+5; push @numbers, $number; } push @data, [@numbers]; @numbers = 0; } my $graph = new GD::Graph::bars(); $graph->set( title => 'Skill Resolution', x_label => 'Skill Level', y_label => 'Success Level' ); my $gd = $graph->plot(\@data) or die "Could not plot: ".$graph->error. Dumper(@data); my $img = $gd->png(); $m->clear_buffer; $r->content_type("image/png"); $r->send_http_header; binmode STDOUT; $m->out($img); $m->abort(OK); # make sure nothing else gets sent </%init>

    Neil Watson
    watson-wilson.ca