13

我想使用jpegoptimoptipng压缩用户通过回形针上传的图像。

我有一个回形针模型配置为:

  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调整大小,这样就只有一个压缩过程?怎么做?

问题2:我打算通过after_post_process回调来做,我可以从中获取三个文件的所有实例,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 回答
2

还有一些宝石可以优化回形针图像:

于 2015-04-26T18:05:26.247 回答
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 回答