Skip to content

Commit

Permalink
Add analyze methods for getting metadata
Browse files Browse the repository at this point in the history
Besides processing images it would be nice to analyze images as well to
get metadata like: width, height and rotation.
This is currently based on the ActiveStorage::Analyzer::ImageAnalyzer.
  • Loading branch information
p8 committed Sep 21, 2023
1 parent 709bcf7 commit d49df1c
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
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

0 comments on commit d49df1c

Please sign in to comment.