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.


In reply to Imager - Problem with Filehandle (Catalyst, DBIx::Class::InflateColumn::FS) by scrivener

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.