Sha256: 68c2e0b0666c711c6566bf6b00e0f88af8b8085494d647f4b8bcd43b516c33d7

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 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
      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`') do |v|
        OptionHelpers.limit_with_range(v.to_i, 0..100)
      end

      # Run first [-1] if max_quality < 100 otherwise with normal priority
      def run_order
        max_quality < 100 ? -1 : 0
      end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
image_optim-0.17.1 lib/image_optim/worker/jpegoptim.rb
image_optim-0.17.0 lib/image_optim/worker/jpegoptim.rb
image_optim-0.16.0 lib/image_optim/worker/jpegoptim.rb
image_optim-0.15.0 lib/image_optim/worker/jpegoptim.rb
image_optim-0.14.0 lib/image_optim/worker/jpegoptim.rb