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

hi all hope ye can help im a newbie to perl and im trying save a png image that i created using gD::graph the following is the code i have
sub graph { my $graph = new GD::Graph::lines(500,350); my $gd_image = $graph->plot(\@data); # print header(-type => "image/png"); # binmode STDOUT; # print $gd_image->png; open (TMPFILE,">file.png"); binmode TMPFILE; 131 print TMPFILE $gd_image->png; close TMPFILE; }
thje following is the error i recieve Can't call method "png" on an undefined value at /var/www/cgi-bin/Findit.cgi line 131. line 131 is marked on the code above this function is to be called multiple times..

Replies are listed 'Best First'.
Re: save a png image
by Tomte (Priest) on Mar 09, 2004 at 16:07 UTC

    You have to check for errors occuring during graph-creation:

    my $gd_image = $graph->plot(\@data);
    should be
    my $gd_image = $graph->plot(\@data) or die $graph->error;
    If an error occures, $gd_image will be undefined, resulting in the error you experience.

    die according to the GD::Graph-docs and you will probably be told were the error lies...

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      i have the lines above it commented out above itcos the graph is being created but it wont display multiple images to the screen so i trying to save them and display them embedded in html so that the user can see many images on the page.. tanx for the advice ill add that to the code though

        Maybe some Monks don't mind as much as I do, but please, please, please at least try to write coherently. Capitalization would help. Perhaps breaking up your big, long, breathless sentence with this neat little thing I like to call a "period." It would help, I promise.

Re: save a png image
by Juerd (Abbot) on Mar 09, 2004 at 16:36 UTC

    As someone else pointed out, you should check for errors. Not only with $graph->plot but also with open, binmode, print and close. These all return undef on failure and have the error in $!.

    open TMPFILE, ">file.png" or die $!; binmode TMPFILE or die $!; print TMPFILE $db_image->png or die $!; close TMPFILE or die $!;

    This is the bare minimum of error checking and reporting that you should do. It is perhaps a good idea to make full sentences: "Could not open file.png: $!\n".

    Alternatively, you can use the Fatal module to wrape open, binmode and close:

    use Fatal qw(open binmode close); open my $tmpfile, ">file.png"; binmode $tmpfile; print $tmpfile $db_image->png or die $!; close $tmpfile;
    Note that I used a lexical filehandle here. Globals are bad for many reasons. See Coping with scoping for information about that.

    When you have done all this, it's time to take indentation lessons :) In fact, you should probably just get a good Perl book or tutorial to learn from. (Like Beginning Perl or this tutorial or one of the many non-free resources out there.)

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      sorry forgive my indentation and untidyness but i cracking up ere about this and it driving me nuts
      my $gd_image = $graph->plot(\@data) or die $graph->error; print header(-type => "image/png") or die $!; binmode STDOUT or die $!; print $gd_image->png or die $!;
      i would give up on the save to file thing if i could get this code to display multiple images on the one screen i doing a project similar to mrtg for collage and i want to be able to display multiple graphs as they do.. using the above code i get no errors but i only get shown the first graph