Page 1 of 1
Initialize Image from string?
Posted: 2011-01-26T15:38:39-07:00
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.
Re: Initialize Image from string?
Posted: 2011-01-26T16:12:24-07:00
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.
Re: Initialize Image from string?
Posted: 2011-01-28T11:11:22-07:00
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?
Re: Initialize Image from string?
Posted: 2011-01-30T09:34:16-07:00
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
Re: Initialize Image from string?
Posted: 2011-01-30T11:57:23-07:00
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();