Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

GD malformed header error

by peppiv (Curate)
on Apr 04, 2002 at 13:50 UTC ( #156654=perlquestion: print w/replies, xml ) Need Help??
peppiv has asked for the wisdom of the Perl Monks concerning the following question:

I had GD installed on the server(FreeBSD) and haven't been able to get a script to run yet. Here's a piece of code the ISP offered, but even they can't run it.

#!/usr/local/bin/perl use GD; Context: "text/html\n\n"; # create a new image $im = new GD::Image(100,100); # allocate some colors $white = $im->colorAllocate(255,255,255); $black = $im->colorAllocate(0,0,0); $red = $im->colorAllocate(255,0,0); $blue = $im->colorAllocate(0,0,255); # make the background transparent and interlaced $im->transparent($white); $im->interlaced('true'); # Put a black frame around the picture $im->rectangle(0,0,99,99,$black); # Draw a blue oval $im->arc(50,50,95,75,0,360,$blue); # And fill it with red $im->fill(50,50,$red); # make sure we are writing to a binary stream binmode STDOUT; # Convert the image to PNG and print it on standard output print $im->png;

Here's the error it produces in my error log.

Thu Apr 4 06:23:58 2002 access to /cgi-bin/ failed for XX.XXX.XXX.XX, reason: malformed header from script. Bad header=‰PNG

I've also tried GD:Graph with this code:

#!/usr/bin/perl use strict; use GD::Graph::bars; my @data = ( ["Jan-01","Feb-01","Mar-01", "Apr-01","May-01","Jun-01","Jul-01","Au +g-01","Sep-01"], [21,25,33,39,49,48,40,45,15] ); my $graph = new GD::Graph::bars; $graph->set( x_label => 'Month', y_label => 'Revenue ($1,000s)', title => 'Monthly Online Sales for 2001', bar_spacing => 10 ) or warn $graph->error; $graph->plot(\@data) or die $graph->error; open(GRAPH,">graph1.jpg") || die "Cannot open graph1.jpg: $!\n"; print GRAPH $graph->gd->jpeg(100);

It produces the same type of error. I CPAN'd GD:Graph and it says it's up to date. What am I doing wrong? Has anyone come across a problem like this?



Replies are listed 'Best First'.
Re: GD malformed header error
by projekt21 (Friar) on Apr 04, 2002 at 14:08 UTC

    When running a CGI you need to output (print) what the browser shall see. Normally this is HTML-Code, but plain text or images are fine, too.

    First of all you need to print a header containing the content-type and two newlines:

    print "Content-type: image/png\n\n"

    ... and remove that line saying: Context: ....

    This should work for a trial.

    I would further encourage you to use strict;, enable warnings (-w) AND use CGI;.

    #!/usr/bin/perl -w use strict; use CGI; my $q = CGI->new; print $q->header("image/png"); ## later print $im->png;

    I'm sure, GD is not to blame.

    alex pleiner <>
    zeitform Internet Dienste

Re: GD malformed header error
by RMGir (Prior) on Apr 04, 2002 at 14:07 UTC
    The problem is that you're printing out the image directly to the output stream, without a "Content-type" prefix.

    The Content-type prefix is needed so the browser knows what it's dealing with, and webservers get annoyed if a CGI script doesn't output them correctly :)

    Adapted from the GD::Graph docs:

    use CGI qw(:standard); #... print header("image/png"); binmode STDOUT; print $im->png;
    I think that will probably do it.

    (Edit: Clarified what Content-type means)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://156654]
Approved by rob_au
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-07-20 15:04 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (437 votes). Check out past polls.