ruby-on-rails-3 - 回形针后期处理 - 如何使用 jpegoptim/optpng 压缩图像

标签 ruby-on-rails-3 paperclip

我想用 jpegoptimoptipng压缩用户通过 Paperclip 上传的图像。

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

  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 ?或者我只是检查扩展字符串是否包含在该字符串中是否安全?

最佳答案

由于没有其他答案,以下是我在我的项目中的做法,而且它似乎可以正常工作几个月。

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

关于ruby-on-rails-3 - 回形针后期处理 - 如何使用 jpegoptim/optpng 压缩图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10495478/

相关文章:

ruby-on-rails - Rails 迁移中的临时索引名称太长

ruby-on-rails-3 - Rails 范围不等于

ruby-on-rails - 如何在 Rails 3 中实现批量插入

mysql - HABTM 查询帮助 (Rails 3)

ruby-on-rails - Rails 4 - Paperclip - 使用 DATA URI 上传图像

ruby-on-rails - 回形针 + RSpec : content_type validation

css - PaperClip ruby​​ on rails 上传时图像大小不一致

ruby-on-rails - `fetch' : key not found: "S3_BUCKET_NAME" (KeyError) with paperclip/aws s3/rails

ruby-on-rails-4 - Rails 4 Action Mailer - 附加非本地内联图像(带有 URL)

Ruby Rails - Assets 的 RoutingError