简体   繁体   English

如何在Ruby中给定URL中的base64编码媒体

[英]How to encode media in base64 given URL in Ruby

I'm trying to upload an image to PingFM. 我正在尝试将图像上传到PingFM。 Their documentation says: 他们的文件说:

media – base64 encoded media data.

I can access this image via the URL. 我可以通过URL访问此图像。 I tried (practically guessed) this: 我试过(几乎猜到了)这个:

ActiveSupport::Base64.encode64(open("http://image.com/img.jpg"))

But I get this error: 但我得到这个错误:

TypeError: can't convert Tempfile into String
    from /usr/lib/ruby/1.8/base64.rb:97:in `pack'
    from /usr/lib/ruby/1.8/base64.rb:97:in `encode64'
    from (irb):19
    from :0

To encode a file: 要编码文件:

require 'base64'
Base64.encode64(File.open("file_path", "rb").read)

To produce the file from the encoded string: 要从编码的字符串生成文件:

require 'base64'
encoded_string = Base64.encode64(File.open("file_path", "rb").read)

File.open(file_name_to_create, "wb") do |file|
    file.write(Base64.decode64(encoded_string))
end

The open method: open方法:

open("http://image.com/img.jpg")

is returning a Tempfile object, while encode64 expects a String. 正在返回一个Tempfile对象,而encode64需要一个String。

Calling read on the tempfile should do the trick: 调用tempfile上的read应该可以解决问题:

ActiveSupport::Base64.encode64(open("http://image.com/img.jpg") { |io| io.read })

This will work too, it's a little cleaner 这也会起作用,它有点清洁

 require 'base64'

 Base64.encode64(open("file_path").to_a.join)

"How do you decode this back into a file?" “你如何将其解码回文件?” - @user94154 - @ user94154

 require 'base64'

 open('output_file_name.txt', 'w') do |f| 
   f << Base64.decode64( encoded_content )
 end

Where encoded_content would be the previously encoded file content return value. 其中encoded_content将是先前编码的文件内容返回值。

Encode a file to base64 encoding: 将文件编码为base64编码:

File.open("output_file","w"){|file| file.write [open("link_to_file").string].pack("m")}

Decode base64 encoded file: 解码base64编码文件:

File.open('original', 'wb') {|file| file << (IO.readlines('output_file').to_s.unpack('m')).first }

Here's my solution: 这是我的解决方案:

1: Put this custom image_tag method into ApplicationHelper, and include ActiveSupport module 1:将此自定义image_tag方法放入ApplicationHelper,并包含ActiveSupport模块

module ApplicationHelper
  include ActiveSupport
  def image_tag_base64(file_path, mime_type = 'image/jpeg', options = {})
    image_tag("data:#{mime_type};base64,#{Base64.encode64(open(file_path) { |io| io.read })}", options)
  end
end

2: Then, inside the view you want to use base64 encoded image use the method like this: 2:然后,在视图内部您要使用base64编码的图像使用如下方法:

<%= image_tag_base64 @model.paperclip_attribute.path(:size), @model.paperclip_attribute.content_type, {class: 'responsive-img etc etc'} %>

3: DONE 3:完成

In case it's useful to others, here's how to save a screenshot as base64 using Watir 如果它对其他人有用,这里是如何使用Watir将屏幕截图保存为base64

browser = Watir::Browser.new(:chrome, {:chromeOptions => {:args => ['--headless', '--window-size=1000x1000']}})
browser.goto("http://www.yourimage.com")
browser.screenshot.base64

The beauty of this is you don't need to store the image itself 这样做的好处是你不需要存储图像本身

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

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