On Wed, 20 Mar 2002, Nicolai Schlenzig wrote: > Date: Wed, 20 Mar 2002 15:19:32 +0100 (CET) > From: Nicolai Schlenzig <[EMAIL PROTECTED]> > To: [EMAIL PROTECTED] > Subject: [MASON] Using GD for dynamic graphics > > Hi > > I would like to add some dynamic generated graphics to my site and find GD being the >one to use. However I'm not sure _how_ to use it on my pages. > > I use HTML::Mason for content management and I am very happy and truly amazed of the >powers in Mason, but when I tried adding some GD it got me puzzled. > > When I create a new GD::Image drawing whatever on it and trying to print it - it >will be prepended to my html header for the page. I then tried to put it in $m->out >to have in printed within Mason, but that simply printed the raw PNG in all its glory >as text on screen. > > I tried fiddeling with content_type, but I guess that wont work if I set this after >I have already printed something on the page - hence the content_type will not even >be effective for that page. > > What I still miss doing/trying is making a seperate file/component which only job >will be to make the PNG, but I don't really see what I would gain from that, because >that will pretty much also just print a PNG "within" my HTML page. > > Like: > > <html> > <head> > </head> > <body> > Some standard html text... > <%perl> > $m->content_type( "image/png" ); > #my Mason/perl code for creating an image with GD. > my $image = GD::Image(); > #put something on image... been using both clean GD synopsis and some home cooked >small widgets. > print $image->png; > #or > $m->out( $image->png ); > #or > $r->print( $image->png ); > $m->content_type( "text/html" ); > </%perl> > Some more html text. > </body> > </html> > > Very simplified, but I don't have my code in front of me - sorry. I > would assume that content_type in only good for setting in the > header, but I don't see how I can mix in this PNG then. > > If GD could virtualle create a file I guess <img src="<% $image->png > %>"> would work... but that seems like a long shot when reading the > man for GD :) > > Any help/hints will be greatly appriciate. Maybe I just need to look > harder for more ressources, but I've already spent a lot of time on > something that looks quite simple - but haven't shown results yet. > > > Best regards > > Nicolai Schlenzig > nicolai.schlenzig(at)got2get.net
Nicolai, I've never used Mason, but I do use GD in my current project for drawing dynamic images. I have a straight-up mod_perl handler that gets the arguments required for drawing the image. It creates the image and writes it to a temporary directory (in something like "/tmp/foo/images/"). I then use Template Toolkit to create an HTML page that has an <img> tag pointing to the temporary file. The temp directory been aliased into my httpd.conf as something like "/images/foo/". I have a cron job that deletes old images. Before coming up with the above solution, I was actually passing the arguments to a separate handler that printed out the image dynamically (i.e., never writing it out to disk). My <img> tag pointed to this handler, and it sent a header of "image/jpeg" (or "image/png" or whatever) and then printed the output of GD->jpeg (or png or whatever). This solution proved not to be the optimal solution for other unrelated reasons, but you can do it this way, too. HTH, ky