Mixing HTML::Template and GD::Graph

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have developed an application using CGI::Application and HTML::Template.
Some of the run modes in the application use GD:Graph to present charts.

In the GD:Graph documentation they simply write graphical information to
STDOUT to present the information to a web page. I can't work out whether
this is possible using HTML::Template, and if it is, how to do it. As a
result I am writing the charts to a file and using a standard image tag in
my template file.

Is it possible to write present a GD:Graph chart without writing a file in
this scenario?

Colin Walls
Removed the pink meat to mail me

Re: Mixing HTML::Template and GD::Graph

Colin Walls wrote:
Quoted text here. Click to load it

Yes, but probably not in the way you're thinking. There's no getting
around the fact that the user agent will be making a second, entirely
separate request for the image data. So you can't print the image and
HTML as part of the same request.

What you can do is, within the HTML::Template CGI, construct an img
element whose src attribute points to a second CGI that prints the
image, and includes in the query string any parameters that one will
need to do its job.


Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

Re: Mixing HTML::Template and GD::Graph

As someone else said, the browser makes a separate request for files

I use GD::Graph on my site a lot. I use it for drawing cute math

For example, I could plot a formula x[1,2] = (-b+-sqrt( b^2-4ac ) )/2a
and it would be painted, you can play with it at

http://www.algebra.com/services/rendering /

It is done without writing to files. Note declaration my $jpeg and
print $jpeg. (as a complication, these jpegs are saved into my binary
cache MySQL table for future reuse).

sub main() {
  my $jpeg;

  print $cgi->header(-type=>'image/jpeg', -expires=>'+1d');

  # try to look up the binary cache right away!
  my $formula_id = "formula: $expr";

  my $use_cache = 1;
  if( $use_cache ) {
    $jpeg = retrieve_item_from_binary_cache( $formula_id );
    if( defined $jpeg ) {
      print $jpeg;
  $full_expr = $expr;
  #print STDERR "$0: Rendering '$expr'.\n";
  my $parsed = create_parsed_expression( $expr );

  my $im;
  if ( !$global_error ) {
    calculate_sizes( $parsed );
    $im = paint( $parsed );
  } else {                      # error
    $im = paint_error_message( $global_error );

  if ( $cgi->param( "cross" ) ) {
    $im->line( 0, 0, $parsed->, $parsed->, $red );
    $im->line( 0, $parsed->, $parsed->, 0, $red );
  $jpeg = $im->jpeg( 70 );
  print $jpeg;

  store_item_in_binary_cache( $formula_id, "formula", $jpeg );

Site Timeline