anarres: (Default)
anarres ([personal profile] anarres) wrote in [site community profile] dw_dev_training2010-06-26 09:30 am
Entry tags:

Dynamic images in Dreamwidth

This seems like it should be easy but I'm really stuck. I'm trying to dynamically display images - i.e. generate the image with a perl script and display it without saving it to the server.

This simple example works on my Heliohost server, but not on Dreamwidth (the example is from http://www.perlmonks.org/?node_id=18565):

--------test.shtml------------
<img src="test.cgi?size=100" alt="" />
------------------------------


--------test.cgi------------
#!/usr/bin/perl -w

use strict;
use CGI;
use GD;

my $cgi=new CGI;
my $cgi_size=$cgi-&gt;param('size') || '50';

print &quot;Content-type: image/gif\n\n&quot;;
my $gd=new GD::Image($cgi_size,$cgi_size);
my $blue=$gd-&gt;colorAllocate(0,0,255);
$gd-&gt;fill(0,0,$blue);
binmode STDOUT; #just in case we're on NT
print $gd-&gt;gif;
------------------------------


I'm stumped - has anyone got any ideas?
exor674: Computer Science is my girlfriend (Default)

[personal profile] exor674 2010-06-26 07:53 am (UTC)(link)
Dreamwidth environments don't actually execute perl / .cgi scripts under htdocs.

http://hg.dwscoalition.org/dw-free/file/cfe6c71e5611/cgi-bin/DW/Controller/CommentCount.pm is an example page that generates an image.

ETA: that sample uses Image::Magick, but you could use GD just as easy
Edited 2010-06-26 07:54 (UTC)
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2010-06-26 07:49 pm (UTC)(link)
Yeah - as exor674 said, best way is to write a controller for a set of URLs representing the different charts. I'd put them in appropriate places for the pages they relate to, eg images for site stats accessible through http://www.dreamwidth.oerg/stats/foo, etc. Also need to decide who's responsible for extracting data. (controller for each image vs. controller for page itself, passing parameters to image congtroller through URL parameters.)

Going to look over the patches you uploaded, for things I need to point out that may still be applicable. Probably tomorrow.
Edited 2010-06-26 19:51 (UTC)
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2010-06-27 04:07 pm (UTC)(link)
Something like that. stats.tt would output (at the appropriate location in the HTML source) something along the lines of <img src="http://www.dreamwidth.org/stats/piechart?free=2&amp;paid=3&amp;premium=5&amp;seed=7" alt=Pie chart: 2 free accounts, 3 paid, 5 premium paid, 7 seed" /> (URL scheme and item names meant as an example only).

:hands you some Tylenol.
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2010-06-30 04:09 pm (UTC)(link)
To the handler in the controller module. You can retrieve it with the following code in the handler:
my $r = DW::Request->get;
my $free = $r->get_args->{free};
For more information, see http://wiki.dwscoalition.org/notes/Routing_and_Template_Toolkit#Creating_a_handler_function
fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)

[personal profile] fu 2010-06-27 07:04 am (UTC)(link)
Hmm, try $r->print( $gd->png );

It's erroring because it thinks you're trying to call $r as a function, with this line: $r->();
fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)

[personal profile] fu 2010-06-28 12:34 am (UTC)(link)
Great! You're welcome *g*