Use paperclip for saving base64 images obtained from an api
To answer my own question, here is what I've come up with:
class Photo < ActiveRecord::Base
before_validation :set_image
has_attached_file :image, styles: { thumb: "x100>" }
validates_attachment :image, presence: true, content_type: { content_type: ["image/jpeg", "image/jpg"] }, size: { in: 0..10.megabytes }
def set_image
StringIO.open(Base64.decode64(image_json)) do |data|
data.class.class_eval { attr_accessor :original_filename, :content_type }
data.original_filename = "file.jpg"
data.content_type = "image/jpeg"
self.image = data
end
end
end
image_json is a text field containing the actual base64 encoded image (just the data part, eg "/9j/4AAQSkZJRg...")
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
base 64 photo to paperclip rails image
This seems to work, but I am still working on testing differing image types.
@new_image = Stand.create(:user_id => @user.id)
encoded_picture = params[:image_data]
content_type = "image/jpg"
image = Paperclip.io_adapters.for("data:#{content_type};base64,#{encoded_picture}")
image.original_filename = "new_image.jpg"
@new_image.avatar = image
@new_image.save
@new_image.cover_image_url = @new_image.avatar.url
@new_image.save
I have upvoted the previous answer because it helped me find other resources, but it does not contain an answer for my scenario.
base64 photo and paperclip -Rails
Make sure that the StringIO you are using is the paperclip opened one. https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/upfile.rb
sio = StringIO.new(Base64.decode64(string))
puts sio.respond_to?(:original_filename)
puts sio.respond_to?(:content_type)
It needs to have those methods in order to have paperclip work with StringIO. Make sure it is setting them.
Upload base64 Image with Paperclip - Rails 4
Assuming that you're using the Rails form helpers over in your view, and based on your form_params
list, the :base64
key won't be at the top level of your params
hash, but rather one level down at params[:form][:base64]
base64 decoding by paperclip in rails 3
In order to save image with correct extension you have to specify content type.
It's quite nice to have this in your model as method which is called before_validation
StringIO.open(Base64.decode64(self.photo_base64)) do |data|
data.original_filename = "image_name.jpg"
data.content_type = "image/jpeg"
self.photo = data
end
Related Topics
What Is an Illegal Octal Digit
Generate a Powerset of a Set Without Keeping a Stack in Erlang or Ruby
Differencebetween <%= ... %> and <% ... %> in Ruby on Rails
Convert Time to Other Timezone
Heroku and Rails: Gem Load Error with Postgres, However It Is Specified in Gemfile
Why Does Ruby /[[:Punct:]]/ Miss Some Punctuation Characters
Why Isn't 'Method=' Treated the Same as Any Other Method
Call Controller from Rake Task
Base64 Photo and Paperclip -Rails
Is There Goto Statement in Ruby
Merging Multi-Dimensional Hashes in Ruby
How to Reverse a 'Rails Generate'
How to Run a Single Rspec Test
Update Just One Gem with Bundler