Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use paperclip for saving base64 images obtained from an api

I have a Photo model with an image attribute. The image contains a base64 string obtained from an api. I need to run an after_create callback and I was thinking I could use Paperclip for saving the image to the disk in the callback as it would save me some work implementing the folder structure in the public folder and generating thumbnails. Is there an easy way to do that?

like image 238
Marek Příhoda Avatar asked May 15 '14 10:05

Marek Příhoda


4 Answers

As of Paperclip 5.2 you need to register the DataUriAdapter for Paperclip to handle base64 images for you.

In config/initializers/paperclip put: Paperclip::DataUriAdapter.register

Then as @eldi says you can just do:

Photo.new(
   image:  "data:image/jpeg;base64,#{image_json}",
   image_file_name: 'file.jpg' # this way you can provide file_name
 )

(See Paperclip release notes here)

like image 131
Ollie H-M Avatar answered Oct 16 '22 18:10

Ollie H-M


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...")

like image 42
Marek Příhoda Avatar answered Nov 19 '22 21:11

Marek Příhoda


your set_image should look something like this

    def set_image        
       self.update({image_attr: "data:image/jpeg;base64," + image_json[PATH_TO_BASE64_DATA]})
    end
like image 6
Jason White Avatar answered Nov 19 '22 20:11

Jason White


At least with Paperclip 5 it works out of the box you need to provide base64 string with format data:image/jpeg;base64,#{base64_encoded_file}

For you model it will be

Photo.new(
   image:  "data:image/jpeg;base64,#{image_json}",
   image_file_name: 'file.jpg' # this way you can provide file_name
 )

Additionally in your controller you do not need to change anything:-) (maybe you would like to accept :image_file_name in params)

like image 3
eldi Avatar answered Nov 19 '22 20:11

eldi