In my current app, I’m using paperclip for handling images.
Paperclip includes the option for id_partition, but I prefer an hashed stucture (descriped in my first blog).
For example, the image url looks like
http://my_url/medias/0d/898/0d898414092854eacbf27f8db12ce4db_avatar.jpg
This is the basic configuration in my Media-Model
# paperclip
has_attached_file( :source,
:styles => {
:original => '1600x1600>',
:avatar => '50x50#' },
:storage => :filesystem,
:url => "/:class/:hashed_public_id/:public_id_:style.:extension",
:path => ":rails_root/public/:class/:hashed_public_id/:public_id_:style.:extension"
)
To set the public id, I used the following code and create an md5-hash.
Surely, you can use what you want, for example ActiveSupport::SecureRandom.hex(32).
My set_public_id creates a random string like “6e374ca829eaead5090f6cdd26c08017″.
def set_public_id
self.public_id = Digest::MD5.hexdigest(self.source_file_name + self.source_file_size.to_s + rand.to_s + Time.now.to_i.to_s)
end
For using hashed_public_id or public_id in the paperclip configuration :url => “/:class/:hashed_public_id/:public_id_:style.:extension”, you must set the following attachment interpolations.
Paperclip::Attachment.interpolations[:public_id] = lambda do |attachment, style|
# e. g. "6e374ca829eaead5090f6cdd26c08017"
attachment.instance.public_id
end
Paperclip::Attachment.interpolations[:hashed_public_id] = lambda do |attachment, style|
# e. g. "6e/374"
File.join(attachment.instance.public_id[0..1], attachment.instance.public_id[2..4])
end
Furthermore, I need to rotate an image. Therefore, I used the follwing code.
All styles of the image (e. g. original, avatar) will be rotated.
require 'RMagick'
def rotate(degrees)
if(degrees.class == Fixnum && degrees % 90 == 0)
each_attachment do |n, a|
self.source.styles.each do |styles|
image_path = a.path(styles.first)
image = Magick::ImageList.new(image_path)
image = image.rotate(degrees)
image.write(image_path)
end
return true
end
end
nil
end
Helpful links:

[...] my last blog about paperclip I described the rotation of images. Since I have added more styles with different [...]
hi, where do you put the ‘def set_public_id…end’ code in?
I put it into the Media-Model and it will be called from the controller in the create action