Sha256: e904b79aa6225e783d138edcbad19b9f8df8f013e59927f87fb170be524e8e2d

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'image_optim/worker'

class ImageOptim
  class Worker
    # http://www.ijg.org/
    #
    # Uses jpegtran through jpegrescan if enabled, jpegrescan is vendored with
    # this gem
    class Jpegtran < Worker
      COPY_CHUNKS_OPTION =
      option(:copy_chunks, false, 'Copy all chunks'){ |v| !!v }

      PROGRESSIVE_OPTION =
      option(:progressive, true, 'Create progressive JPEG file'){ |v| !!v }

      JPEGRESCAN_OPTION =
      option(:jpegrescan, true, 'Use jpegtran through jpegrescan, '\
          'ignore progressive option'){ |v| !!v }

      def used_bins
        jpegrescan ? [:jpegtran, :jpegrescan] : [:jpegtran]
      end

      def optimize(src, dst, options = {})
        if jpegrescan
          args = %W[
            #{src}
            #{dst}
          ]
          args.unshift '-s' unless copy_chunks
          resolve_bin!(:jpegtran)
          execute(:jpegrescan, args, options) && optimized?(src, dst)
        else
          args = %W[
            -optimize
            -outfile #{dst}
            #{src}
          ]
          args.unshift '-copy', (copy_chunks ? 'all' : 'none')
          args.unshift '-progressive' if progressive
          execute(:jpegtran, args, options) && optimized?(src, dst)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
image_optim-0.30.0 lib/image_optim/worker/jpegtran.rb