Sha256: d49bd9a5d7a7aae239da781650df226ec52588b7c649e2d27a43af896c779a27

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

require 'image_optim/worker'
require 'image_optim/option_helpers'

class ImageOptim
  class Worker
    # http://www.kokkonen.net/tjko/projects.html
    class Jpegoptim < Worker
      ALLOW_LOSSY_OPTION =
      option(:allow_lossy, false, 'Allow limiting maximum quality'){ |v| !!v }

      STRIP_OPTION =
      option(:strip, :all, Array, 'List of extra markers to strip: '\
          '`:comments`, '\
          '`:exif`, '\
          '`:iptc`, '\
          '`:icc` or '\
          '`:all`') do |v|
        values = Array(v).map(&:to_s)
        known_values = %w[all comments exif iptc icc]
        unknown_values = values - known_values
        unless unknown_values.empty?
          warn "Unknown markers for jpegoptim: #{unknown_values.join(', ')}"
        end
        values & known_values
      end

      MAX_QUALITY_OPTION =
      option(:max_quality, 100, 'Maximum image quality factor '\
          '`0`..`100`, ignored in default/lossless mode') do |v, opt_def|
        if allow_lossy
          OptionHelpers.limit_with_range(v.to_i, 0..100)
        else
          if v != opt_def.default
            warn "#{self.class.bin_sym} #{opt_def.name} #{v} ignored " \
                'in lossless mode'
          end
          opt_def.default
        end
      end

      TIMEOUT_OPTION = timeout_option

      # Run earlier if max_quality is less than 100
      def run_order
        max_quality < 100 ? -1 : 0
      end

      def optimize(src, dst)
        src.copy(dst)
        args = %W[
          --quiet
          --
          #{dst}
        ]
        strip.each do |strip_marker|
          args.unshift "--strip-#{strip_marker}"
        end
        args.unshift "--max=#{max_quality}" if max_quality < 100
        execute(:jpegoptim, *args) && optimized?(src, dst)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
discourse_image_optim-0.26.1 lib/image_optim/worker/jpegoptim.rb
discourse_image_optim-0.24.5 lib/image_optim/worker/jpegoptim.rb
discourse_image_optim-0.24.4 lib/image_optim/worker/jpegoptim.rb