Sha256: e4f6ed1fbe7dd37985df69a15c1c97680291a5c0cbf27f7e6a9f88e9f8bb1eeb

Contents?: true

Size: 1.19 KB

Versions: 28

Compression:

Stored size: 1.19 KB

Contents

module Paperclip
  class GeometryDetector
    def initialize(file)
      @file = file
      raise_if_blank_file
    end

    def make
      geometry = GeometryParser.new(geometry_string.strip).make
      geometry || raise(Errors::NotIdentifiedByImageMagickError.new)
    end

    private

    def geometry_string
      begin
        orientation = Paperclip.options[:use_exif_orientation] ?
          "%[exif:orientation]" : "1"
        Paperclip.run(
          "identify",
          "-format '%wx%h,#{orientation}' :file", {
            :file => "#{path}[0]"
          }, {
            :swallow_stderr => true
          }
        )
      rescue Cocaine::ExitStatusError
        ""
      rescue Cocaine::CommandNotFoundError => e
        raise_because_imagemagick_missing
      end
    end

    def path
      @file.respond_to?(:path) ? @file.path : @file
    end

    def raise_if_blank_file
      if path.blank?
        raise Errors::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")
      end
    end

    def raise_because_imagemagick_missing
      raise Errors::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
    end
  end
end

Version data entries

28 entries across 26 versions & 6 rubygems

Version Path
kt-paperclip-4.4.0 lib/paperclip/geometry_detector_factory.rb
paperclip-5.2.1 lib/paperclip/geometry_detector_factory.rb
paperclip-5.2.0 lib/paperclip/geometry_detector_factory.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/paperclip-5.1.0/lib/paperclip/geometry_detector_factory.rb
paperclip-5.1.0 lib/paperclip/geometry_detector_factory.rb
paperclip-5.0.0 lib/paperclip/geometry_detector_factory.rb
paperclip-4.3.7 lib/paperclip/geometry_detector_factory.rb
paperclip_jk-5.0.0.beta2 lib/paperclip/geometry_detector_factory.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/paperclip-4.3.6/lib/paperclip/geometry_detector_factory.rb
paperclip-5.0.0.beta2 lib/paperclip/geometry_detector_factory.rb
paperclip-5.0.0.beta1 lib/paperclip/geometry_detector_factory.rb
paperclip-4.3.6 lib/paperclip/geometry_detector_factory.rb
paperclip-4.3.5 lib/paperclip/geometry_detector_factory.rb
paperclip-4.3.4 lib/paperclip/geometry_detector_factory.rb
paperclip-4.3.3 lib/paperclip/geometry_detector_factory.rb
paperclip-4.3.2 lib/paperclip/geometry_detector_factory.rb
paperclip-4.3.1 lib/paperclip/geometry_detector_factory.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/paperclip-4.2.4/lib/paperclip/geometry_detector_factory.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/paperclip-4.2.4/lib/paperclip/geometry_detector_factory.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.3/lib/paperclip/geometry_detector_factory.rb