Sha256: 4b912a0a1d064710e69f8c4de2292aac75694a869bc78851ecbe77b95f2768b9

Contents?: true

Size: 949 Bytes

Versions: 6

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

require 'image_optim/worker'
require 'exifr/jpeg'

class ImageOptim
  class Worker
    # http://www.sentex.net/~mwandel/jhead/
    #
    # Jhead internally uses jpegtran which should be on path
    class Jhead < Worker
      ORIENTED = (2..8).freeze # not top-left

      # Works on jpegs
      def image_formats
        [:jpeg]
      end

      # Run first, while exif is still present
      def run_order
        -10
      end

      def used_bins
        [:jhead, :jpegtran]
      end

      def optimize(src, dst, options = {})
        return false unless oriented?(src)

        src.copy(dst)
        args = %W[
          -autorot
          #{dst}
        ]
        resolve_bin!(:jpegtran)
        execute(:jhead, args, options) && dst.size?
      end

    private

      def oriented?(image)
        exif = EXIFR::JPEG.new(image.to_s)
        ORIENTED.include?(exif.orientation.to_i)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
image_optim-0.31.4 lib/image_optim/worker/jhead.rb
image_optim-0.31.3 lib/image_optim/worker/jhead.rb
image_optim-0.31.2 lib/image_optim/worker/jhead.rb
image_optim-0.31.1 lib/image_optim/worker/jhead.rb
image_optim-0.31.0 lib/image_optim/worker/jhead.rb
image_optim-0.30.0 lib/image_optim/worker/jhead.rb