如何使用 ruby​​ 将 base64 字符串保存为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1160741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:30:48  来源:igfitidea点击:

How to save a base64 string as an image using ruby

ruby-on-railsrubyimagefile-iobase64

提问by Ricky

I'm integrating my Ruby on Rails app with a usps shipping system. Once you make a postage request, you pay for that postage and it's nonrefundable.

我正在将我的 Ruby on Rails 应用程序与 usps 运输系统集成。一旦您提出邮资请求,您就需要支付该邮资并且不予退还。

Postage requests will return you an xml response including a base64 string, which is the shipping label.

邮资请求将返回一个 xml 响应,其中包含一个 base64 字符串,即发货标签。

I'm able to render the shipping label in a view, however to make it foolproof, I would like to be able to save that base64 string as an image on my server in the event that something happens to the shipping label between generation (paying for it) and mailing so it may be reprinted without buying a new one.

我能够在视图中呈现运输标签,但是为了使其万无一失,我希望能够将该 base64 字符串保存为我的服务器上的图像,以防在一代之间运输标签发生问题(付费为它)并邮寄,以便无需购买新的即可重印。

My first thoughts were as follows

我的第一个想法如下

# Attempt 1
File.open('shipping_label.gif', 'w+') {|f|
    f.puts Base64.decode64(base_64_encoded_data)
}

# Attempt 2
File.open('shipping_label.gif', 'w+') {|f|
    f.puts Base64.decode64(Base64.decode64(base_64_encoded_data))
}

Neither work.

都不工作。

回答by tadman

When writing binary data to a file, such as is the case with an image, using IO#puts is hazardous and best avoided. You should be writing in binary mode, which is mostly irrelevant on LF-only platforms such as UNIX or OS X, but is imperative on CRLF ones such as Windows. IO#puts also appends a newline at the end of the file which is invalid.

将二进制数据写入文件时,例如图像的情况,使用 IO#puts 是危险的,最好避免。您应该以二进制模式编写,这在 UNIX 或 OS X 等仅 LF 平台上几乎无关紧要,但在 CRLF 平台(例如 Windows)上是必不可少的。IO#puts 还在文件末尾附加一个无效的换行符。

The best approach is to specify the correct flag on the open call:

最好的方法是在 open 调用中指定正确的标志:

File.open('shipping_label.gif', 'wb') do |f|
  f.write(Base64.decode64(base_64_encoded_data))
end

For example, see the comment on the IO#open documentation page:

例如,请参阅 IO#open 文档页面上的评论:

http://apidock.com/ruby/IO/open/class

http://apidock.com/ruby/IO/open/class

回答by Piotr Kuczynski

Other answers are pretty close, but usually assume that base64 stream will contain PNG data. This is not always the case so I suggest to use mime types library to establish correct file extension:

其他答案非常接近,但通常假设 base64 流将包含 PNG 数据。情况并非总是如此,因此我建议使用 MIME 类型库来建立正确的文件扩展名:

REGEXP = /\Adata:([-\w]+\/[-\w\+\.]+)?;base64,(.*)/m

data_uri_parts = data_url.match(REGEXP) || []
extension = MIME::Types[data_uri_parts[1]].first.preferred_extension
file_name = "myfilename.#{extension}"

File.open(file_name, 'wb') do |file|
    file.write(Base64.decode64(data_uri_parts[2]))
end

回答by stopanko

require 'RMagick'
data = params[:image_text]# code like this  data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABPUAAAI9CAYAAABSTE0XAAAgAElEQVR4Xuy9SXPjytKm6ZwnUbNyHs7Jc7/VV9bW1WXWi9q
image_data = Base64.decode64(data['data:image/png;base64,'.length .. -1])
new_file=File.new("somefilename.png", 'wb')
new_file.write(image_data)

After you kan use image as file Photo.new(image: image)#save useng paperclip in Photo model

在你可以使用图像作为文件 Photo.new(image: image)#save useng paperclip in Photo 模型之后

回答by Corban Brook

If you need to write it to an image then use imagemagick through the rmagick gem.

如果您需要将其写入图像,请通过 rmagick gem 使用 imagemagick。

http://rmagick.rubyforge.org/

http://rmagick.rubyforge.org/