Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analyze methods for getting metadata #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/image_processing.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "image_processing/chainable"
require "image_processing/builder"
require "image_processing/pipeline"
require "image_processing/analyzer"
require "image_processing/processor"
require "image_processing/version"

Expand Down
12 changes: 12 additions & 0 deletions lib/image_processing/analyzer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ImageProcessing
# Abstract class inherited by individual analyzers.
class Analyzer
def initialize(image)
@image = image
end

def analyze
{ width: @image.width, height: @image.height, rotated: rotated? }
end
end
end
21 changes: 21 additions & 0 deletions lib/image_processing/mini_magick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ def self.valid_image?(file)
false
end

def self.analyze(file)
if valid_image?(file)
image = ::MiniMagick::Image.new(file.path)
Analyzer.new(image).analyze
else
{}
end
end

class Analyzer < ImageProcessing::Analyzer
ROTATIONS = %w[ RightTop LeftBottom TopRight BottomLeft ]

private

def rotated?
ROTATIONS.include?(@image["%[orientation]"])
rescue ::MiniMagick::Error
false
end
end

class Processor < ImageProcessing::Processor
accumulator :magick, ::MiniMagick::Tool

Expand Down
22 changes: 22 additions & 0 deletions lib/image_processing/vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module ImageProcessing
module Vips
extend Chainable
ROTATIONS = /Right-top|Left-bottom|Top-right|Bottom-left/

# Returns whether the given image file is processable.
def self.valid_image?(file)
Expand All @@ -15,6 +16,27 @@ def self.valid_image?(file)
false
end

def self.analyze(file)
if valid_image?(file)
image = ::Vips::Image.new_from_file(file.path, access: :sequential)
Analyzer.new(image).analyze
else
{}
end
end

class Analyzer < ImageProcessing::Analyzer
ROTATIONS = /Right-top|Left-bottom|Top-right|Bottom-left/

private

def rotated?
ROTATIONS === @image.get("exif-ifd0-Orientation")
rescue ::Vips::Error
false
end
end

class Processor < ImageProcessing::Processor
accumulator :image, ::Vips::Image

Expand Down
18 changes: 18 additions & 0 deletions test/mini_magick_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@
assert_dimensions [300, 400], pipeline.loader(geometry: "400x400").call
end unless ENV["GM"]

it "analyzes the image" do
result = ImageProcessing::MiniMagick.analyze(@portrait)
expected = { width: 600, height: 800, rotated: false }
assert_equal expected, result
end

it "analyzes the rotated image" do
result = ImageProcessing::MiniMagick.analyze(fixture_image("rotated.jpg"))
expected = { width: 800, height: 600, rotated: true }
assert_equal expected, result
end

it "does not analyze an invalid image" do
result = ImageProcessing::MiniMagick.analyze(fixture_image("invalid.jpg"))
expected = {}
assert_equal expected, result
end

it "auto orients by default" do
result = ImageProcessing::MiniMagick.call(fixture_image("rotated.jpg"))
assert_dimensions [600, 800], result
Expand Down
18 changes: 18 additions & 0 deletions test/vips_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@
assert_dimensions [600, 800], result
end

it "analyzes a image" do
result = ImageProcessing::Vips.analyze(@portrait)
expected = { width: 600, height: 800, rotated: false }
assert_equal expected, result
end

it "analyzes a rotated image" do
result = ImageProcessing::Vips.analyze(fixture_image("rotated.jpg"))
expected = { width: 800, height: 600, rotated: true }
assert_equal expected, result
end

it "does not analyze an invalid image" do
result = ImageProcessing::Vips.analyze(fixture_image("invalid.jpg"))
expected = {}
assert_equal expected, result
end

it "applies loader options" do
result = ImageProcessing::Vips.loader(shrink: 2).call(@portrait)
assert_dimensions [300, 400], result
Expand Down
Loading