open()ing strings as filehandles

J

J. Sharp

Hello all,

I'm trying to use ImageMagick to create a thumbnail from an image (JPEG) that I
have as a string returned from an SQL query. Image::Magick->Read() will accept
either a filename or an open file handle, so I'm opening the string as and
passing it a reference (?) to it.

my $immk = Image::Magick->new();

open(FH, "<", \$image);
$immk->Read(file => \*FH);
close(FH);

$immk->Thumbnail(width => THUMBNAIL_WIDTH);

open(FH, ">", \$image);
$immk->Write(file => \*FH, filename => 'image.jpg');
close(FH);

When I'm done, I expect to have the thumbnailed image in $image, but I have
NULL! If I try this with real files, everything works fine:

my $immk = Image::Magick->new();
$immk->Read('/tmp/01.jpg');
$immk->Thumbnail(width => THUMBNAIL_WIDTH);
$immk->Write('/tmp/thumbnailed.jpg');

I really have no clue what I'm doing wrong. Thoughts, anyone?

Thanks.
 
U

Uri Guttman

JS> open(FH, ">", \$image);
JS> $immk->Write(file => \*FH, filename => 'image.jpg');
JS> close(FH);

JS> When I'm done, I expect to have the thumbnailed image in $image,
JS> but I have NULL! If I try this with real files, everything works
JS> fine:

JS> my $immk = Image::Magick->new();
JS> $immk->Read('/tmp/01.jpg');
JS> $immk->Thumbnail(width => THUMBNAIL_WIDTH);
JS> $immk->Write('/tmp/thumbnailed.jpg');

there you have a single arg as the filename. above you have both a
handle AND a filename. try dropping the filename as it isn't needed. you
want it to write to the string handle only. the docs show both args for
a write call which makes no sense. so i bet the docs are in error.

$image->Write(file=>\*IMAGE, filename=>$filename);

in fact if it can detect a handle vs a filename you may just be able to
pass the handle by itself (no key) and it may work. a single arg seems
to be assumed the file or filename. worth trying anyhow.

uri
 
X

Xho Jingleheimerschmidt

J. Sharp said:
Hello all,

I'm trying to use ImageMagick to create a thumbnail from an image (JPEG) that I
have as a string returned from an SQL query. Image::Magick->Read() will accept
either a filename or an open file handle, so I'm opening the string as and
passing it a reference (?) to it.

my $immk = Image::Magick->new();

open(FH, "<", \$image);
$immk->Read(file => \*FH);
close(FH);

Image::Magick is a wrapper around C code. I don't know exactly where
the boundaries of this wrapper are, but it is Perl-unaware C code doing
the reading and writing, then Perl's magic filehandles aren't going to work.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,183
Members
46,719
Latest member
login dogas.info

Latest Threads

Top