srchulo
srchulo

Reputation: 5203

Perl Image::Magick get in-memory contents

I'm using Image::Magick to modify my images. I am then using an HTTP::Request to send the image content to an API.

HTTP::Request has a content method which allows you to set the content for the request, but obviously this requires that you have the content in memory.

I know that I can read the content of the image into a variable by opening a file and reading it. However, since Image::Magick already has the content of the image in memory, is there any way that I can get it via my Image::Magick object? Thanks!

Upvotes: 3

Views: 828

Answers (1)

Borodin
Borodin

Reputation: 126722

Image::Magick keeps the image in a custom format in memory to make it more simple and efficient to manipulate. It has to compress the data to JPEG, PNG, or whatever format you request before it is written to a file, so you cannot just access the image in memory as it stands.

However, the module's ImageToBlob method will provide an in-memory copy of the data it would have written to disk, to save you writing it out and reading it back again.

Note that it returns a list of images to allow for an object that contains more than one frame, so if you have only a single frame you must write

my @blobs = $image->ImageToBlob;
$request->content($blobs[0]);

or

my ($blob) = $image->ImageToBlob;
$request->content($blob);

Upvotes: 5

Related Questions