简体   繁体   English

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

[英]How to save a base64 string as an image using ruby

I'm integrating my Ruby on Rails app with a usps shipping system.我正在将我的 Ruby on Rails 应用程序与 usps 运输系统集成。 Once you make a postage request, you pay for that postage and it's nonrefundable.一旦您提出邮资请求,您就需要支付该邮资并且不予退还。

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.都不工作。

When writing binary data to a file, such as is the case with an image, you cannot use text printing tools like IO#puts .将二进制数据写入文件时(例如图像的情况),您不能使用IO#puts等文本打印工具。

There's two things you need to ensure:您需要确保两件事:

  • You need to be writing in binary mode to avoid any possible LF to CRLF expansion.您需要以二进制模式写入以避免任何可能的 LF 到 CRLF 扩展。
  • You must use write instead of puts as write can work on arbitrary data, but puts (literally "put string") is exclusively for text.您必须使用write而不是puts因为write可以处理任意数据,但puts (字面意思是“放置字符串”)专门用于文本。

Combining these you get:结合这些你得到:

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

Other answers are pretty close, but usually assume that base64 stream will contain PNG data.其他答案非常接近,但通常假设 base64 流将包含 PNG 数据。 This is not always the case so I suggest to use mime types library to establish correct file extension:情况并非总是如此,因此我建议使用 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
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 can use image as a file Photo.new(image: image)#save using paperclip in Photo model在您可以将图像用作文件后 Photo.new(image: image)#save 在照片模型中使用回形针

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM