Ruby on Rails - Base64的外部图像URL

时间:2015-12-07 14:22:20

标签: ruby-on-rails ruby paperclip

无论如何,Ruby on Rails中是否存在将图像URL(https://meo-fb-natal-dev.s3.amazonaws.com/participations/cropped/56658c5de2fc7116340000c0/635769180217506883-GD5A9264.jpg?1449495643)中托管的图像转换为Base64的内容?

我尝试过很多东西,但似乎都没有。

2 个答案:

答案 0 :(得分:7)

究竟什么不起作用?

以下是一个例子:

$> irb
>> require 'open-uri'
>> img = open("https://meo-fb-natal-dev.s3.amazonaws.com/participations/cropped/56658c5de2fc7116340000c0/635769180217506883-GD5A9264.jpg?1449495643")
# img variable is a tempfile with image
>> require 'base64'
>> Base64.encode64(img.read)
=> "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJ\nChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/\n2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo\nKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAFTAoYDASIAAhEBAxEB/8QA\nHAAAAAcBAQAAAAAAAAAAAAAAAQIDBAUGBwAI/8QAShAAAgEDAgQEAwUGB..."

答案 1 :(得分:0)

你可以试试这个

url = "https://meo-fb-natal-dev.s3.amazonaws.com/participations/cropped/56658c5de2fc7116340000c0/635769180217506883-GD5A9264.jpg?1449495643"
ActiveSupport::Base64.encode64(open(url) { |io| io.read })