How to encode media in base64 given URL in Ruby

Ruby on-RailsRubyBase64EncodeActivesupport

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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 })

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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 }

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRamon TayagView Question on Stackoverflow
Solution 1 - Ruby on-RailsMarielyn AlvaradoView Answer on Stackoverflow
Solution 2 - Ruby on-RailsmtyakaView Answer on Stackoverflow
Solution 3 - Ruby on-RailsrobertodecurnexView Answer on Stackoverflow
Solution 4 - Ruby on-RailskhelllView Answer on Stackoverflow
Solution 5 - Ruby on-Railsmld.oscarView Answer on Stackoverflow
Solution 6 - Ruby on-RailsstevecView Answer on Stackoverflow