13

ユーザーが Paperclip 経由でアップロードした画像を使用jpegoptimまたは圧縮したいと考えています。optipng

次のように構成されたペーパークリップ モデルがあります。

  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: ユーザーがアップロードした元の画像を圧縮してから、Paperclip でサイズを変更して、圧縮プロセスを 1 回だけにすることはできますか? そしてそれを行う方法は?

質問 2: コールバック経由で実行しようとしていafter_post_processます。3 つのファイルのすべてのインスタンスを から取得できimage.queued_for_write、ファイル拡張子で jpegoptim/optipng をトリガーしたいのですが、 を使用するcurrent_format = File.extname(file.path)と、次のような結果が得られます.jpg20120508-7991-cqcpf2。拡張文字列を取得する方法はありますjpgか? または、拡張文字列がその文字列に含まれているかどうかを確認するだけで安全ですか?

4

3 に答える 3

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
于 2012-08-30T15:51:15.800 に答える
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 に圧縮しました!!!

于 2013-10-04T10:11:10.557 に答える