Skip to content

Instantly share code, notes, and snippets.

@WizardOfOgz
Created June 7, 2011 12:13
Show Gist options
  • Save WizardOfOgz/1012107 to your computer and use it in GitHub Desktop.
Save WizardOfOgz/1012107 to your computer and use it in GitHub Desktop.
Save Base64-encoded images with Paperclip
class Avatar < ActiveRecord::Base
attr_accessor :content_type, :original_filename, :image_data
before_save :decode_base64_image
has_attached_file :image,
PAPERCLIP_CONFIG.merge(
:styles => {
:thumb => '32x32#',
:medium => '64x64#',
:large => '256x256#'
})
protected
def decode_base64_image
if image_data && content_type && original_filename
decoded_data = Base64.decode64(image_data)
data = StringIO.new(decoded_data)
data.class_eval do
attr_accessor :content_type, :original_filename
end
data.content_type = content_type
data.original_filename = File.basename(original_filename)
self.image = data
end
end
end
@MohamedHegab
Copy link

@sarink I've include Paperclip::DataUriAdapter.register in paperclip.rb and still don't upload the image

@bbugh
Copy link

bbugh commented May 24, 2018

This is so easy!

If you're on paperclip 5.1.0 you don't need to use the Paperclip::DataUriAdapter.register, it's still included by default. I believe they only changed this with 5.2.0

@Leoxxid
Copy link

Leoxxid commented Nov 21, 2018

Here it is saving only original format, any suggestion?
Paperclip 5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment