Sha256: 99fa5dc2d95266de188576cace000feafbd261a6b23e39f922bd96ce67250f0c

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

require 'image_optim/worker'

class ImageOptim
  class Worker
    # http://www.lcdf.org/gifsicle/
    class Gifsicle < Worker
      # If interlace specified initialize one instance
      # Otherwise initialize two, one with interlace off and one with on
      def self.init(image_optim, options = {})
        return super if options.key?(:interlace)

        [false, true].map do |interlace|
          new(image_optim, options.merge(:interlace => interlace))
        end
      end

      INTERLACE_OPTION =
      option(:interlace, false, TrueFalseNil, 'Interlace: '\
          '`true` - interlace on, '\
          '`false` - interlace off, '\
          '`nil` - as is in original image '\
          '(defaults to running two instances, one with interlace off and '\
          'one with on)') do |v|
        TrueFalseNil.convert(v)
      end

      LEVEL_OPTION =
      option(:level, 3, 'Compression level: '\
          '`1` - light and fast, '\
          '`2` - normal, '\
          '`3` - heavy (slower)') do |v|
        OptionHelpers.limit_with_range(v.to_i, 1..3)
      end

      CAREFUL_OPTION =
      option(:careful, false, 'Avoid bugs with some software'){ |v| !!v }

      TIMEOUT_OPTION = timeout_option

      def optimize(src, dst)
        args = %W[
          --output=#{dst}
          --no-comments
          --no-names
          --same-delay
          --same-loopcount
          --no-warnings
          --
          #{src}
        ]

        if resolve_bin!(:gifsicle).version >= '1.85'
          args.unshift '--no-extensions', '--no-app-extensions'
        end

        unless interlace.nil?
          args.unshift interlace ? '--interlace' : '--no-interlace'
        end
        args.unshift '--careful' if careful
        args.unshift "--optimize=#{level}" if level
        execute(:gifsicle, *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/gifsicle.rb
discourse_image_optim-0.24.5 lib/image_optim/worker/gifsicle.rb
discourse_image_optim-0.24.4 lib/image_optim/worker/gifsicle.rb