Initialize Image from string?

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
geordie
Posts: 2
Joined: 2011-01-26T13:47:07-07:00
Authentication code: 8675308

Initialize Image from string?

Post by geordie »

Is it possible to use a string to initialize an Image? I have a cgi that receives a bunch of files in a POST request some of which are PNG files. The current workflow is all those files are saved into a zip file with Archive::Zip. I would like to make sure the PNGs do not have transparency before putting them in the zip file.

The current workflow looks like this:

Code: Select all

...
my $newimageData = MIME::Base64::decode($aPNG);
#---->Where transparency needs to be removed
my $newmember = $zip->addString( $newimageData,  $pathInZip );
....
Is there a way I can do that without having to write the png to disk, rereading it in with PerlMagick, removing the transparency, re-rewriting it back to disk with PerlMagick, and finally re-re-reading it in to save it into the zip file? Along with the added complexity in the code and having to deal with creating temporary folders and files it just feels wrong to have to do all that reading and writing.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Initialize Image from string?

Post by magick »

Take a look at the INLINE image format. Look for INLINE in the image format tables @ http://www.imagemagick.org/script/formats.php.
geordie
Posts: 2
Joined: 2011-01-26T13:47:07-07:00
Authentication code: 8675308

Re: Initialize Image from string?

Post by geordie »

That will save me the earlier step of base64 decoding it myself but I don't think it answers the question I was asking. I need to know how to initialize an Image from a variable instead of a file. In other words I am looking for something akin to:

$image->ReadImageFromString("$myPNG");
or if I don't pre-decode it myself and use the raw INLINE as you suggest
$image->ReadImageFromString("$myBase64EncodedString");

Does such a thing exist?
michaelUFL
Posts: 5
Joined: 2010-07-30T14:54:36-07:00
Authentication code: 8675308

Re: Initialize Image from string?

Post by michaelUFL »

I am looking to do the same thing ... read the image directly from a perl string instead of having to write it out to a file.

I have tried the following unsuccessfully:

Code: Select all

    open FH, '<', \$content;
    my $numRead = $magick->Read(file=>\*FH);
I am wondering whether or not the issue is related to having to specify the image format (e.g. jpg) for ImageMagick.

I tried (unsuccessfully) to specify the format using:

Code: Select all

    $magick->Read(magick=>'JPEG', file=>\*FH);
No luck.

Advice / guidance appreciated.

Michael
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Initialize Image from string?

Post by magick »

Here's an example of reading an image from a string with PerlMagick:

Code: Select all

use Image::Magick;

local $/=undef;
open IMAGE, "logo.png" or die "Couldn't open file: $!";
binmode IMAGE;
$logo = <IMAGE>;
close IMAGE;

$image=Image::Magick->new();
$image->Read(blob=>$logo);
$image->Display();
Post Reply