Problem converting PNG to JPEG
Posted: 2008-04-25T11:41:16-07:00
I'm trying to use PerlMagick to convert a PNG to a JPEG, in memory, and I want to return the resulting BLOB. Although I can do a command line conversion without any problems, in code the image always comes out as a PNG. Here's what the code looks like:
Is it possible to do this at all? If it is, is the above even close to correct, or am I missing something really simple?
If someone has working code, I'd appreciate a posting - thanks!
Code: Select all
my $image = Image::Magick->new;
# $imagebuffer is the BLOB of the image
if( 1 != $image->BlobToImage( $imagebuffer ) ) {
undef $imagebuffer;
$log->logcroak("2:ImageMagick:BlobToImage() failed: $!");
}
# convert non jpeg images to jpeg
unless ($imgmagick eq "JPEG") {
if ($imgmagick eq "PNG" || $imgmagick eq "GIF") {
# Going for lossless conversion
$image->Set(quality=>100,compression=>'none',magick=>'jpeg');
$image->Strip();
}
}
$jpegRawImageBuffer = $image->ImageToBlob();
return $jpegRawImageBuffer;
If someone has working code, I'd appreciate a posting - thanks!