rails3中Paperclip使用ImageMagick处理中文名图片

在model中:

has_attached_file :photo,
:styles => { :small => '200x200>' },
:withy => false,
:url => "/attachments/:attachment/#{curr_date}/:style/:normalized_photo_file_name",
:path => ":rails_root/public/attachments/:attachment/#{curr_date}/:style/:normalized_photo_file_name"

Paperclip.interpolates :normalized_photo_file_name do |attachment, style|
attachment.instance.normalized_photo_file_name
end

def normalized_photo_file_name
"#{self.photo_file_name.gsub( /[^a-zA-Z0-9_\.]/, '_')}"
end


添加如下内容,主要是为了重新命名文件
before_create :create_random_file_name

def create_random_file_name
extension = File.extname(photo_file_name).downcase
self.photo.instance_write(:file_name, "#{Time.now.strftime("%Y%m%d%H%M%S")}#{rand(10000)}#{extension}")
end

 

转载自:http://www.oschina.net/question/92992_52302

 

你可能感兴趣的:(Rails,Paperclip)