2015-01-07 184 views
1

我想将图像发送到我的服务器,它是base64编码的(减去关于数据的起始位:image/jpeg; base64,)。将base64字符串转换为perl图像magick中的图像

我想在制作缩略图之后上传图像 - 我正在使用图像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

不知道如何在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); 
相关问题