在 perl 中使用 image magick 将 base64 字符串转换为图像 [英] convert base64 string to image with image magick in perl

查看:24
本文介绍了在 perl 中使用 image magick 将 base64 字符串转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的服务器发送一个 base64 编码的图像(减去关于 data:image/jpeg;base64, 的起始位).

I am trying to send an image to my server which is base64 encoded (minus the starting bit about data:image/jpeg;base64, ).

我想在制作缩略图后上传图片 - 我正在使用图片魔法.

I want to upload the image after making a thumbnail of it - I am using image magick.

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

How do I get image magick to read my base64 string as an image, so that I can modify and save it?

到目前为止我的代码:

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);

编辑:有这个工作,上面的代码实际上是正确的,对我来说问题出在别处!

EDIT: have got this working, code above is actually correct, the problem was elsewhere for me !

推荐答案

不确定在 Image::Magick 中是怎么做的,我可以建议 MIME::Base64,这是一个核心模块.

Not sure how it's done in Image::Magick, may I suggest MIME::Base64, it's a core module.

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);

这篇关于在 perl 中使用 image magick 将 base64 字符串转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆