anarres (
anarres) wrote in
dw_dev_training2010-06-26 09:30 am
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[site community profile]](https://www.dreamwidth.org/img/comm_staff.png)
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->param('size') || '50';
print "Content-type: image/gif\n\n";
my $gd=new GD::Image($cgi_size,$cgi_size);
my $blue=$gd->colorAllocate(0,0,255);
$gd->fill(0,0,$blue);
binmode STDOUT; #just in case we're on NT
print $gd->gif;
------------------------------
I'm stumped - has anyone got any ideas?
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->param('size') || '50';
print "Content-type: image/gif\n\n";
my $gd=new GD::Image($cgi_size,$cgi_size);
my $blue=$gd->colorAllocate(0,0,255);
$gd->fill(0,0,$blue);
binmode STDOUT; #just in case we're on NT
print $gd->gif;
------------------------------
I'm stumped - has anyone got any ideas?
no subject
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
no subject
Going to look over the patches you uploaded, for things I need to point out that may still be applicable. Probably tomorrow.
no subject
no subject
:hands you some Tylenol.
no subject
If I navigate to piechart?free=2, does the information free=2 get passed to piechart.tt or to the controller module Piechart.pm?
no subject
no subject
sub commentcount_handler {
my $r = DW::Request->get;
my $args = $r->get_args;
my $count = $args->{samplecount} || 0;
my $entry = undef;
if ( $args->{ditemid} && $args->{user} ) {
my $ditemid = $args->{ditemid};
my $uid = LJ::get_userid( $args->{user} );
$entry = LJ::Entry->new( $uid, ditemid => $ditemid ) if $uid;
}
$count = $entry->reply_count if $entry;
# create an image
my $image = Image::Magick->new;
$image->Set(pen => 'black');
$image->Set(font => 'Generic.ttf');
$image->Set(pointsize => 12);
$image->Set(size => '30x12');
$image->Read("label:$count");
# return the image
$r->content_type("image/png");
$r->print( $image->ImageToBlob( magick => "png" ) );
return $r->OK;
}
So I created a new controller module and attempted to write a version of this subroutine which would create an image with GD instead of with Image::Magick:
#!/usr/bin/perl
package DW::Controller::GDImage;
use strict;
use warnings;
use DW::Routing;
use GD;
use GD::Image;
DW::Routing->register_string("/test", \&my_subroutine, app => 1);
sub my_subroutine {
my $r = DW::Request->get;
# create an image
my $gd=new GD::Image;
my $blue=$gd->colorAllocate(0,0,255);
$gd->fill(0,0,$blue);
# return the image
$r->content_type("image/png");
$r->( print( $gd->png ) );
return $r->OK;
}
1;
This doesn't work. What's giving me problems is the line
$r->( print( $gd->png ) );
Which produces a page showing the error '[Error: Not a CODE reference at /dreamhack/home/8214-anarres/dw/cgi-bin/DW/Controller/GDImage.pm line 23. @ hack.dreamwidth.net]' - I take it this means that the thing in parentheses after the word 'print' should be a reference to a subroutine, but I'm not sure what I should do about this. At the top of the page are a bunch of symbols and letters starting with 'PNG' so I guess the images is being created, but somehow is being displayed as raw data rather than as an image.
For comparison, the version of this that works in a non-Dreamwidth environment (the one in my original post is a bit wrong) is:
#!/usr/bin/perl -w
use GD::Image;
print "Content-type: image/png\n\n";
my $gd=new GD::Image;
my $blue=$gd->colorAllocate(0,0,255);
$gd->fill(0,0,$blue);
print $gd->png;
no subject
It's erroring because it thinks you're trying to call $r as a function, with this line:
$r->();
no subject
no subject