scrivener has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I'm trying to use Imager to scale an Image, which I get from a Catalyst upload request. Then I want to store the original image, an image of medium quality and a thumbnail via DBIx::Class::InflateColumn::Fs on the Filesystem.
The following code works perfect (just storing the original image without using Imager):
my $fh = $c->req->upload('original')->fh; $self->form->item->original($fh); $self->form->item->update; # The DBIx::Class row object comes from FormHandler, # but thats not important here
Now I try to use Imager to get my thumbnail and medium image. This is probably pretty naive:
my $fh = $c->req->upload('original')->fh; binmode $fh; my $img = Imager->new(fh => $fh) or die "Cannot read $fh ", $img->errstr; my $medium = $img->scale(xpixels => 640, ypixels => 640, type => 'min' +); my $thumb = $img->scale(xpixels => 200, ypixels => 200, type => 'min') +; $self->form->item->medium_pfad($medium); $self->form->item->thumb_pfad($thumb); $self->form->item->original($fh); $self->form->item->update;
Everything seems to go fine, but the generated images are corrupted, even the original. I've tried numerous variants...
The only way I could get this to work was writing the files to disk first an reading them in again:
$img->write(file => 'a.jpg'); $medium->write(file => 'b.jpg'); $thumb->write(file => 'c.jpg'); $fho = new IO::File "> a.jpg"; $fhm = new IO::File "> b.jpg"; $fht = new IO::File "> c.jpg"; # Then use these filehandles...
But this is obviously stupid. I'm sure that I just don't understand how filehandles work in Perl. Any help would be greatly appreciated.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Imager - Problem with Filehandle (Catalyst, DBIx::Class::InflateColumn::FS)
by tonyc (Friar) on Oct 21, 2009 at 00:44 UTC | |
by scrivener (Initiate) on Oct 21, 2009 at 01:58 UTC | |
by scrivener (Initiate) on Oct 21, 2009 at 11:01 UTC |