Sha256: 5f148ad98fe9b86e01bdd6796aa764ec9d854676e2b0d552318b8477e735e438

Contents?: true

Size: 1.18 KB

Versions: 11

Compression:

Stored size: 1.18 KB

Contents

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

class ImageOptim
  class Worker
    # https://github.com/danielgtaylor/jpeg-archive#jpeg-recompress
    class Jpegrecompress < Worker
      ALLOW_LOSSY_OPTION =
      option(:allow_lossy, false, 'Allow worker, it is always lossy'){ |v| !!v }

      # Initialize only if allow_lossy
      def self.init(image_optim, options = {})
        super if options[:allow_lossy]
      end

      QUALITY_NAMES = [:low, :medium, :high, :veryhigh].freeze

      quality_names_desc = QUALITY_NAMES.each_with_index.map do |name, i|
        "`#{i}` - #{name}"
      end.join(', ')

      QUALITY_OPTION =
      option(:quality, 3, "JPEG quality preset: #{quality_names_desc}") do |v|
        OptionHelpers.limit_with_range(v.to_i, 0...QUALITY_NAMES.length)
      end

      def used_bins
        [:'jpeg-recompress']
      end

      # Run early as lossy worker
      def run_order
        -5
      end

      def optimize(src, dst)
        args = %W[
          --quality #{QUALITY_NAMES[quality]}
          --no-copy
          #{src}
          #{dst}
        ]
        execute(:'jpeg-recompress', *args) && optimized?(src, dst)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
image_optim-0.26.3 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.26.2 lib/image_optim/worker/jpegrecompress.rb
discourse_image_optim-0.26.2 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.26.1 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.26.0 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.25.0 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.24.3 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.24.2 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.24.1 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.24.0 lib/image_optim/worker/jpegrecompress.rb
image_optim-0.23.0 lib/image_optim/worker/jpegrecompress.rb