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

该用户已被和谐 发布于 2012/05/05 19:55
阅读 979
收藏 0

换进是 Windows下的,

当前是这样的:

    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

但是在windows下如果图片名字是中文就没有办法处理,会报错。而英文和数字之类的名字没有问题。

An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: C:/Users/outman/AppData/Local/Temp/中文名字.png20120505-4140-1kv2vk9 is not recognized by the 'identify' command.

加载中
返回顶部
顶部