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

If I store an image in MySQL in a table with a column of type BLOB. When I go to read it back to manipulate it with Image::Magick its going to be in a variable. This kind of presents a problem. Image::Magick->new() only takes a file name to grab the image, if I have read the manual correctly. How would I make a variable act like a filehandle so Image::Magick would take the image? Or have I completely missed the instructions and Image::Magick will recognize the data as an image format and I would have no problem using the variable?

Thanks,
BMaximus

please forgive the badly formatted question.
  • Comment on Image::Magick and reading images from variables.

Replies are listed 'Best First'.
Re: Image::Magick and reading images from variables.
by andye (Curate) on Mar 27, 2001 at 18:31 UTC
    Hi BMaximus,

    You want to use BlobToImage(), like this:

    my $image=Image::Magick->new(magick=>'gif'); $image->BlobToImage($blobdata);
    You probably want to check that what you have *really is* a gif before you do anything with it though, otherwise ImageMagick will crash fairly horribly.

    Also have a look at ImageToBlob() (does the obvious).

    Docs are here: http://www.imagemagick.org/www/perl.html#blobs

    Oh, and if you're using if for the web, it's probably good to have a cacheing proxy in front of it, if it's going to see much use, since ImageMagick is not all that nippy. We use Squid.

    andy.

    Update: I don't know why I assumed it was a gif. If the image could be more than one format, you could presumably call Image::Magick->new() without parameters, load the blob and use Image::Magick itself to detect the format. I haven't tried this though.

      Thank you very much. I guess at 5am in the morning when your tired and bleary eyed you miss a lot of things. I was looking at that page and I seemed to have missed it.

      BMaximus
Re: Image::Magick and reading images from variables.
by deprecated (Priest) on Mar 27, 2001 at 23:39 UTC
    Probably IO::Scalar would work as well. It allows you to treat a scalar as a FH object. In this case, you could grab your scalar from MySQL, sucked into a scalar, and IO::Scalar can give you a FH that you could slurp into Image::Magick.

    that seems simplest to this monk, but then I love that module.

    brother dep.

    --
    Laziness, Impatience, Hubris, and Generosity.