2012-05-08 42 views
13

我想用jpegoptimoptipng来压缩用户通过回形针上传的图像。回形针后处理 - 如何使用jpegoptim/optpng压缩图像

我已经配置了一个回形针模型:

has_attached_file :image, 
        :styles => {:thumb => '50x50>', :preview => '270x270>' }, 
        :url => "/system/:class/:attachment/:id/:basename_:style.:extension", 
        :path => ":rails_root/public/system/:class/:attachment/:id/:basename_:style.:extension" 

问题1: 是否有可能通过压缩用户上载的原始图像,然后让回形针调整其大小,所以只有一个压缩过程?以及如何做到这一点?

问题2: 我打算通过after_post_process回调做它,我能得到的三个文件的所有实例从image.queued_for_write,我想触发jpegoptim /使用OptiPNG通过文件的扩展名,但是当我使用current_format = File.extname(file.path) ,我得到这样的:.jpg20120508-7991-cqcpf2。有没有拿到扩展字符串jpg?或者是安全的,我只是检查扩展字符串是否包含在该字符串中?

+0

这个问题的任何消息吗? – CharlieMezak

+0

@CharlieMezak我贴了自己的答案,因为没有其他答案。我可能不是最好的,我很高兴听到你对此有任何评论。谢谢。 – larryzhao

回答

4

由于没有其他的答案,这里是我如何在我的项目中做到这一点,它似乎正在工作好几个月。

class AttachedImage < ActiveRecord::Base 
    belongs_to :attachable, :polymorphic => true 

    validates_attachment_presence :image 
    validates_attachment_content_type :image, :content_type=>['image/jpeg', 'image/png', 'image/gif'] 

    has_attached_file :image, 
        :styles => {:thumb => '50x50>', :preview => '270x270>' }, 
        # :processors => [:image_compressor], 
        :url => "/system/:class/:attachment/:id/:basename_:style.:extension", 
        :path => ":rails_root/public/system/:class/:attachment/:id/:basename_:style.:extension" 


    after_post_process :compress 

    private 
    def compress 
    current_format = File.extname(image.queued_for_write[:original].path) 

    image.queued_for_write.each do |key, file| 
     reg_jpegoptim = /(jpg|jpeg|jfif)/i 
     reg_optipng = /(png|bmp|gif|pnm|tiff)/i 

     logger.info("Processing compression: key: #{key} - file: #{file.path} - ext: #{current_format}") 

     if current_format =~ reg_jpegoptim 
     compress_with_jpegoptim(file) 
     elsif current_format =~ reg_optipng 
     compress_with_optpng(file) 
     else 
     logger.info("File: #{file.path} is not compressed!") 
     end 
    end 
    end 

    def compress_with_jpegoptim(file) 
    current_size = File.size(file.path) 
    Paperclip.run("jpegoptim", "-o --strip-all #{file.path}") 
    compressed_size = File.size(file.path) 
    compressed_ratio = (current_size - compressed_size)/current_size.to_f 
    logger.debug("#{current_size} - #{compressed_size} - #{compressed_ratio}") 
    logger.debug("JPEG family compressed, compressed: #{ '%.2f' % (compressed_ratio * 100) }%") 
    end 

    def compress_with_optpng(file) 
    current_size = File.size(file.path) 
    Paperclip.run("optipng", "-o7 --strip=all #{file.path}") 
    compressed_size = File.size(file.path) 
    compressed_ratio = (current_size - compressed_size)/current_size.to_f 
    logger.debug("#{current_size} - #{compressed_size} - #{compressed_ratio}") 
    logger.debug("PNG family compressed, compressed: #{ '%.2f' % (compressed_ratio * 100) }%") 
    end        
end 
1

可能是一个性能妥协,但我用FFMPEG或AVCONV获得更好的压缩图像。

sudo apt-get install ffmpeg 

=初始

Paperclip.options[:command_path] = "/usr/bin/" # see `which ffmpeg` 

=莫代尔

after_save :compress_with_ffmpeg 

def compress_with_ffmpeg 
    [:thumb, :original, :medium].each do |type| 
    img_path = self.avtar.path(type) 
    Paperclip.run("ffmpeg", " -i #{img_path} #{img_path}") 
    end 
end 

我得到了1.7MB的图像压缩302.9KB!