in reply to Use of GD::Graph

You can print either HTML or an image. If you mix it, it will fail miserably.

You say you want to print an image, so do that - but don't print HTML also (you use start_html and end_html)

You're also printing two headers, remove the first one (only print the image header, not the HTML one).

If you want to print both an image and HTML, split the script up into two scripts - one that emits the HTML which contains an <img src="http://url.to/other/perl/script.pl"> that points to the second script.

Replies are listed 'Best First'.
Re^2: Use of GD::Graph
by campbell (Beadle) on May 07, 2008 at 08:39 UTC
    Oh. I didn't realise that. I'll try splitting the script in two.

    Thanks very much for your quick reply.

    Campbell

      Right. I've tried creating the graph object, and passing it in a URL to a second Perl script like so:

      1st Perl script:

      use strict; use warnings; use CGI qw/:standard *table *Tr *td/; use CGI::Carp qw(fatalsToBrowser); use Date::Manip; use DBI; use GD::Graph; use GD::Graph::bars; use GD::Graph::lines; use GD::Graph::histogram; ... (snip HTML and form generation code plus database interrogation code) ... my $graph=GD::Graph::bars->new(); my $format=$graph->export_format; $graph->set ( x_label=>'Uploaders', y_label=>'Total number of uploads' ); @bar_data=(\@x-axis, \@bars); my $gd=$graph->plot(\@bar_data)->$format() or die $graph->error; print '<img src=http://foobar.com/foobar.pl?image=$gd&format=$format>' +;#sends the parameters to the second script
      And the second Perl script:
      #!/usr/bin/perl -w use strict; use warnings; use CGI qw(:standard); my $image=param('image'); my $format=param('format'); print header("image/$format"); binmode STDOUT; print $image;
      However, I just get a blank where the image should be when I do this. I know the image object is created properly, but passing it as a parameter just doesn't seem to work.

      Any ideas as to what I'm doing wrong?

        my $gd=$graph->plot(\@bar_data)->$format() or die $graph->error; print '<img src=http://foobar.com/foobar.pl?image=$gd&format=$format>' +;

        $gd is a GD object, not the image itself. And you can't pass the object around as an URL parameter.

        Likewise you can't pass the image itself around as an URL paramter, because it's massive binary data. Instead you have to create the image in the second script. Or you store it on disk in the first script, and just print the URL of the created image into the HTML.