Use paperclip for saving base64 images obtained fr

2020-02-26 07:13发布

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?

5条回答
Summer. ? 凉城
2楼-- · 2020-02-26 07:55

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
查看更多
甜甜的少女心
3楼-- · 2020-02-26 07:57

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)

查看更多
成全新的幸福
4楼-- · 2020-02-26 08:06

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)

查看更多
太酷不给撩
5楼-- · 2020-02-26 08:16

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

查看更多
家丑人穷心不美
6楼-- · 2020-02-26 08:16
require 'RMagick'
data = params[:image_text]# code like this  data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABPUAAAI9CAYAAABSTE0XAAAgAElEQVR4Xuy9SXPjytKm6ZwnUbNyHs7Jc7/VV9bW1WXWi9q
image_data = Base64.decode64(data['data:image/png;base64,'.length .. -1])
new_file=File.new("somefilename.png", 'wb')
new_file.write(image_data)

After you kan use image as file Photo.new(image: image)#save useng paperclip in Photo model

查看更多
登录 后发表回答