Displaying base64 encoded image in rails
The problem was with the ruby code. This worked:
<td><%= ('<img src="data:image/png;base64,%s">' % user.image).html_safe %></td>
How to add base64 images as inline attachment to a mail in ActiveMailer?
Base64 is an encoded representation of the data in a file.
Note the difference between Base64 and DataURL.
DataURL strings start with
data:image/png;base64,
or a similar string. If this is your case, then your base 64 encoded data is everything after the,
:base_64 = content.split(',')[1]
This base 64 data is just the result of reading a file and encoding its data as base 64:
content = Base64.encode64(File.read('your/path.png'))
You only need to undo the encoding to get the exact result as what the function File.read
would return:
data = Base64.decode64(content)
Your method will end as the following:
def inline_base64(name, content)
attachments.inline[name] = Base64.decode64(content)
end
If you are still getting a broken image check if your content
starts with a DataURL string and strip that out.
Upload base64 encoded image with paperclip - Rails
This is my solution, using Paperclip.io_adapters.for(image) where image is base64 string.
def create_image image, image_name, cat
signature = Paperclip.io_adapters.for(image)
base_name = File.basename(image_name,File.extname(image_name))
signature.original_filename = "#{base_name}.jpg"
media_img = Media::Image.new()
media_img.image = signature
media_img.company_id = current_company_id
media_img.type = cat
media_img.save
end
Convert actual image to base64 in Ruby
Converting the binary data to base64 and passing that as data source isn't enough on its own. This would produce:
<img src="asdf...sdf==" />
Which isn't a valid source.
You have to create an data URI:
<%= image_tag("data:image/png;base64,#{Base64.encode64(@image)}") %>
This assumes your data has a MIME type of image/png
. You could make this dynamic, but you have to extract the MIME type from the file or the extension somehow.
You might also be able to leave out the MIME type, since it's optional for data URIs. I'm not sure how the webbrowser would react to this though:
<%= image_tag("data:;base64,#{Base64.encode64(@image)}") %>
You could also create a helper for this. The asset pipeline already has a helper asset_data_uri
. However since you fetch your images dynamically they are not part of the static server assets you might not be able to use it.
Showing base64 data with Ruby on Rails
This is not Base64 encoded data, you should try below code:
require "base64"
<%= Base64.encode64(@photo) %>
Learn more
Related Topics
Does Ruby Support Unicode and How Does It Work
Run a Cli Thor App Without Arguments or Task Name
What Are Tainted Objects, and When Should We Untaint Them
How to Create Temp Dir in Ruby
Dynamically Create Class Attributes with Attr_Accessor
How to Determine Leap Year in Ruby
Cross-Platform Means of Getting User's Home Directory in Ruby
Set Default Stage with Capistrano 3
Bundle Command Not Found Windows X64
How to Run and Debug Ruby on Rails from Visual Studio Code
Ruby Sandboxing VS. Integrating a Scripting Language
How to Beautify Xml Code in Rails Application
Actiondispatch::Http::Uploadedfile.Content_Type Not Being Initialized in Rspec Test
Rspec Testing Has_Many :Through and After_Save
Carrierwave File Upload with Different File Types
Document Model Attributes with Yard
How to Resolve Deprecation Warnings for Openssl::Cipher::Cipher#Encrypt