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

发布于 2024-08-07 12:50:55 字数 548 浏览 8 评论 0原文

我正在尝试将图像上传到 PingFM。他们的文档说:

media – base64 encoded media data.

我可以通过该网址访问此图片。我尝试过(实际上猜到了):

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

但我收到此错误:

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

I'm trying to upload an image to PingFM. Their documentation says:

media – base64 encoded media data.

I can access this image via the 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

氛圍 2024-08-14 12:50:55

对文件进行编码:

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

从编码字符串生成文件:

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

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
你不是我要的菜∠ 2024-08-14 12:50:55

open 方法:

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

返回一个 Tempfile 对象,而 encode64 需要一个 String。

在临时文件上调用 read 应该可以解决问题:

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

The open method:

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

is returning a Tempfile object, while encode64 expects a String.

Calling read on the tempfile should do the trick:

ActiveSupport::Base64.encode64(open("http://image.com/img.jpg") { |io| io.read })
轻拂→两袖风尘 2024-08-14 12:50:55

这也可以工作,它更干净一些

 require 'base64'

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

“你如何将其解码回文件?” - @user94154

 require 'base64'

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

其中 encoded_content 是之前编码的文件内容返回值。

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

 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.

甚是思念 2024-08-14 12:50:55

将文件编码为 Base64 编码:

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

解码 Base64 编码文件:

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

Encode a file to base64 encoding:

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

Decode base64 encoded file:

File.open('original', 'wb') {|file| file << (IO.readlines('output_file').to_s.unpack('m')).first }
豆芽 2024-08-14 12:50:55

这是我的解决方案:

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:然后,在要使用 base64 编码图像的视图内使用如下方法:

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

3:完成

Here's my solution:

1: Put this custom image_tag method into ApplicationHelper, and include ActiveSupport module

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:

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

3: DONE

风追烟花雨 2024-08-14 12:50:55

如果对其他人有用,请参阅以下如何使用 Watir 将屏幕截图保存为 base64 的

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

好处,这样做的好处是您不需要存储图像本身

In case it's useful to others, here's how to save a screenshot as base64 using Watir

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文