-
Notifications
You must be signed in to change notification settings - Fork 22.2k
Expand file tree
/
Copy pathimage_analyzer.rb
More file actions
34 lines (30 loc) · 892 Bytes
/
image_analyzer.rb
File metadata and controls
34 lines (30 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true
module ActiveStorage
# = Active Storage Image \Analyzer
#
# This is an abstract base class for image analyzers, which extract width and height from an image blob.
#
# If the image contains EXIF data indicating its angle is 90 or 270 degrees, its width and height are swapped for convenience.
#
# Example:
#
# ActiveStorage::Analyzer::ImageAnalyzer::ImageMagick.new(blob).metadata
# # => { width: 4104, height: 2736 }
class Analyzer::ImageAnalyzer < Analyzer
extend ActiveSupport::Autoload
autoload :Vips
autoload :ImageMagick
def self.accept?(blob)
blob.image?
end
def metadata
read_image do |image|
if rotated_image?(image)
{ width: image.height, height: image.width }
else
{ width: image.width, height: image.height }
end
end
end
end
end