File tree Expand file tree Collapse file tree 6 files changed +92
-0
lines changed Expand file tree Collapse file tree 6 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 1
1
require "image_processing/chainable"
2
2
require "image_processing/builder"
3
3
require "image_processing/pipeline"
4
+ require "image_processing/analyzer"
4
5
require "image_processing/processor"
5
6
require "image_processing/version"
6
7
Original file line number Diff line number Diff line change
1
+ module ImageProcessing
2
+ # Abstract class inherited by individual analyzers.
3
+ class Analyzer
4
+ def initialize ( image )
5
+ @image = image
6
+ end
7
+
8
+ def analyze
9
+ { width : @image . width , height : @image . height , rotated : rotated? }
10
+ end
11
+ end
12
+ end
Original file line number Diff line number Diff line change @@ -16,6 +16,27 @@ def self.valid_image?(file)
16
16
false
17
17
end
18
18
19
+ def self . analyze ( file )
20
+ if valid_image? ( file )
21
+ image = ::MiniMagick ::Image . new ( file . path )
22
+ Analyzer . new ( image ) . analyze
23
+ else
24
+ { }
25
+ end
26
+ end
27
+
28
+ class Analyzer < ImageProcessing ::Analyzer
29
+ ROTATIONS = %w[ RightTop LeftBottom TopRight BottomLeft ]
30
+
31
+ private
32
+
33
+ def rotated?
34
+ ROTATIONS . include? ( @image [ "%[orientation]" ] )
35
+ rescue ::MiniMagick ::Error
36
+ false
37
+ end
38
+ end
39
+
19
40
class Processor < ImageProcessing ::Processor
20
41
accumulator :magick , ::MiniMagick ::Tool
21
42
Original file line number Diff line number Diff line change 6
6
module ImageProcessing
7
7
module Vips
8
8
extend Chainable
9
+ ROTATIONS = /Right-top|Left-bottom|Top-right|Bottom-left/
9
10
10
11
# Returns whether the given image file is processable.
11
12
def self . valid_image? ( file )
@@ -15,6 +16,27 @@ def self.valid_image?(file)
15
16
false
16
17
end
17
18
19
+ def self . analyze ( file )
20
+ if valid_image? ( file )
21
+ image = ::Vips ::Image . new_from_file ( file . path , access : :sequential )
22
+ Analyzer . new ( image ) . analyze
23
+ else
24
+ { }
25
+ end
26
+ end
27
+
28
+ class Analyzer < ImageProcessing ::Analyzer
29
+ ROTATIONS = /Right-top|Left-bottom|Top-right|Bottom-left/
30
+
31
+ private
32
+
33
+ def rotated?
34
+ ROTATIONS === @image . get ( "exif-ifd0-Orientation" )
35
+ rescue ::Vips ::Error
36
+ false
37
+ end
38
+ end
39
+
18
40
class Processor < ImageProcessing ::Processor
19
41
accumulator :image , ::Vips ::Image
20
42
Original file line number Diff line number Diff line change 106
106
assert_dimensions [ 300 , 400 ] , pipeline . loader ( geometry : "400x400" ) . call
107
107
end unless ENV [ "GM" ]
108
108
109
+ it "analyzes the image" do
110
+ result = ImageProcessing ::MiniMagick . analyze ( @portrait )
111
+ expected = { width : 600 , height : 800 , rotated : false }
112
+ assert_equal expected , result
113
+ end
114
+
115
+ it "analyzes the rotated image" do
116
+ result = ImageProcessing ::MiniMagick . analyze ( fixture_image ( "rotated.jpg" ) )
117
+ expected = { width : 800 , height : 600 , rotated : true }
118
+ assert_equal expected , result
119
+ end
120
+
121
+ it "does not analyze an invalid image" do
122
+ result = ImageProcessing ::MiniMagick . analyze ( fixture_image ( "invalid.jpg" ) )
123
+ expected = { }
124
+ assert_equal expected , result
125
+ end
126
+
109
127
it "auto orients by default" do
110
128
result = ImageProcessing ::MiniMagick . call ( fixture_image ( "rotated.jpg" ) )
111
129
assert_dimensions [ 600 , 800 ] , result
Original file line number Diff line number Diff line change 76
76
assert_dimensions [ 600 , 800 ] , result
77
77
end
78
78
79
+ it "analyzes a image" do
80
+ result = ImageProcessing ::Vips . analyze ( @portrait )
81
+ expected = { width : 600 , height : 800 , rotated : false }
82
+ assert_equal expected , result
83
+ end
84
+
85
+ it "analyzes a rotated image" do
86
+ result = ImageProcessing ::Vips . analyze ( fixture_image ( "rotated.jpg" ) )
87
+ expected = { width : 800 , height : 600 , rotated : true }
88
+ assert_equal expected , result
89
+ end
90
+
91
+ it "does not analyze an invalid image" do
92
+ result = ImageProcessing ::Vips . analyze ( fixture_image ( "invalid.jpg" ) )
93
+ expected = { }
94
+ assert_equal expected , result
95
+ end
96
+
79
97
it "applies loader options" do
80
98
result = ImageProcessing ::Vips . loader ( shrink : 2 ) . call ( @portrait )
81
99
assert_dimensions [ 300 , 400 ] , result
You can’t perform that action at this time.
0 commit comments