使用perl中的image magick将base64字符串转换为图像

时间:2015-01-07 10:50:03

标签: perl api

我正在尝试将图像发送到我的服务器,该服务器是base64编码的(减去关于数据的起始位:image / jpeg; base64,)。

我想在制作缩略图之后上传图片 - 我正在使用image magick。

如何将图像magick作为图像读取我的base64字符串,以便我可以修改并保存它?

到目前为止我的代码:

my $extension = 'jpg';
my $full_filename = $photo_filepath . $cand->id . '.' . $extension;

require Image::Magick;
my $cand_photo = Image::Magick->new;
my $decoded = decode_base64($args{image_string});
$cand_photo->read(blob=>$decoded);

#save original
$cand_photo->Write($full_filename);

#resize
$cand_photo->Set( Gravity => 'Center' );
$cand_photo->Resize( geometry => '120x120' );
$cand_photo->Extent( geometry => '120x120' );
my $full_filename_120
    = $photo_filepath . $cand->id . '_120x120.' . $extension;

#save thumbnail
$cand_photo->Write($full_filename_120);

编辑:有这个工作,上面的代码实际上是正确的,问题在我的其他地方!

1 个答案:

答案 0 :(得分:1)

不确定如何在Image::Magick中完成,我可以建议MIME::Base64,这是一个核心模块。

my $image_decoded= MIME::Base64::decode_base64($image_string);
open (my $handle, '>', 'image_file.jpg') or die $!;
binmode $handle;
print $handle $image_decoded;
close ($handle);
相关问题