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?
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- Use savefig in Python with string and iterative in
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- Where does this quality loss on Images come from?
- form_for wrong number of arguments in rails 4
your set_image should look something like this
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
Additionally in your controller you do not need to change anything:-) (maybe you would like to accept
:image_file_name
inparams
)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:
(See Paperclip release notes here)
To answer my own question, here is what I've come up with:
image_json is a text field containing the actual base64 encoded image (just the data part, eg "/9j/4AAQSkZJRg...")
After you kan use image as file Photo.new(image: image)#save useng paperclip in Photo model