geektron has asked for the wisdom of the Perl Monks concerning the following question:

OK ... according to  perldoc CGI ( i have CGI.pm v2.98 )
To be safe, use the upload() function (new in version 2.47). When called with the name of an upload field, upload() returns a filehandle, or undef if the parameter is not a valid filehandle.

$fh = $query->upload('uploaded_file');
while (<$fh>) {
print;
}

everything looks fine ... but what if you want to do something different with the tmpfile?

I'm trying to pass it into Image::Magick, like so:

my $fh = $self->cgi->upload( 'map' ); my $thumb = Image::Magick->new(); $thumb->Read( file => \*$fh ) or die "No read to Image::Magick ob +j: $! \n";

it always dies on the read.

if  $fh ( according to the docs ) is a filehandle, and Image::Magick expects a glob ref to a filehandle, what am i missing?

i've tried adding the full path to the image. no love.

Replies are listed 'Best First'.
Re: CGI::upload and Image::Magick
by halley (Prior) on Aug 18, 2003 at 15:43 UTC
    http://www.imagemagick.org/www/perl.html#read suggests it just wants a filehandle. A glob ref is just how you turn a bareword into a filehandle for argument passing. Thus, either file => \*STDIN or file => $fh is fine.

    --
    [ e d @ h a l l e y . c c ]

Re: CGI::upload and Image::Magick
by bear0053 (Hermit) on Aug 18, 2003 at 15:47 UTC
    according to the docs: when you perform read it should look like the following:
    use Image::Magick; p = new Image::Magick; p->Read("imagefile"); p->Set(attribute => value, ...) ($a, ...) = p->Get("attribute", ...) p->routine(parameter => value, ...) p->Mogrify("Routine", parameter => value, ...) p->Write("filename");
    this is straight off cpan. try not using the Read( file => \*$fh ) and just use p->Read("imagefile");
Re: CGI::upload and Image::Magick
by tcf22 (Priest) on Aug 18, 2003 at 15:47 UTC
    $fh is a filehandle and that is what Read requires. Not a reference to it.

    Try
    $thumb->Read( file => $fh ) or die "No read to Image::Magick obj: $! +\n";
      right, $fh is already a filehandle. in fact, that wasn't the problem.

      Image::Magick::Read has no return value, so any code like:

             $th->Read( file => $fh ) or die ....

      will fail, because the LHS will never get set to 1.

      this stumped me for a couple days, until i started thinking about why $! wasn't set. that was the clue as to why things didn't work.

      well, i'd already tried that as well, but tried it again. it doesn't work ...

      from the error-log:

      Creating /webcontent/server_home/rentsavers/images/4/map.jpg opening --> map.jpg [Mon Aug 18 10:48:39 2003] [error] No read to Image::Magick obj *:

      need to go back through Image::Magick docs to see why  $! isn't being set.