open()ing strings as filehandles

Discussion in 'Perl Misc' started by J. Sharp, Jul 16, 2009.

  1. J. Sharp

    J. Sharp Guest

    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.
    J. Sharp, Jul 16, 2009
    #1
    1. Advertising

  2. J. Sharp

    Uri Guttman Guest

    >>>>> "JS" == J Sharp <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Jul 16, 2009
    #2
    1. Advertising

  3. J. Sharp wrote:
    > 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
    Xho Jingleheimerschmidt, Jul 17, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Eyles

    Parse-ing Strings

    Martin Eyles, Jan 12, 2006, in forum: ASP .Net
    Replies:
    10
    Views:
    674
    Martin Eyles
    Jan 19, 2006
  2. John

    realloc'ing an array of strings

    John, Jun 28, 2004, in forum: C Programming
    Replies:
    4
    Views:
    328
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    754
    Malcolm
    Jun 24, 2006
  4. J. Sharp
    Replies:
    0
    Views:
    3,122
    J. Sharp
    Jul 15, 2009
  5. Vilmos Soti
    Replies:
    17
    Views:
    257
    Rocco Caputo
    May 9, 2004
Loading...

Share This Page