2012-05-08 23 views
13

またはoptipngを使用して、Paperclip経由でアップロードした画像を圧縮します。ペーパークリップ後処理 - 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 

私は302.9キロバイトに圧縮1.7メガバイトの画像を得ました!

関連する問題