diff options
author | George Claghorn <george.claghorn@gmail.com> | 2017-10-22 13:16:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-22 13:16:59 -0400 |
commit | 605484079d297d1ba6835628465be81f03c052ee (patch) | |
tree | e4e32f5e47f4bf48cb9eb9fc8d80c7da268ae262 /activestorage/lib/active_storage/analyzer/image_analyzer.rb | |
parent | 6525e7fb2e534a6564438008f9c02cf29eb37483 (diff) | |
download | rails-605484079d297d1ba6835628465be81f03c052ee.tar.gz rails-605484079d297d1ba6835628465be81f03c052ee.tar.bz2 rails-605484079d297d1ba6835628465be81f03c052ee.zip |
Extract metadata from images and videos
Diffstat (limited to 'activestorage/lib/active_storage/analyzer/image_analyzer.rb')
-rw-r--r-- | activestorage/lib/active_storage/analyzer/image_analyzer.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/activestorage/lib/active_storage/analyzer/image_analyzer.rb b/activestorage/lib/active_storage/analyzer/image_analyzer.rb new file mode 100644 index 0000000000..b25d2092b9 --- /dev/null +++ b/activestorage/lib/active_storage/analyzer/image_analyzer.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module ActiveStorage + # Extracts width and height in pixels from an image blob. + # + # Example: + # + # ActiveStorage::Analyzer::ImageAnalyzer.new(blob).metadata + # # => { width: 4104, height: 2736 } + # + # This analyzer relies on the third-party [MiniMagick]{https://github.com/minimagick/minimagick} gem. MiniMagick requires + # the [ImageMagick]{http://www.imagemagick.org} system library. These libraries are not provided by Rails; you must + # install them yourself to use this analyzer. + class Analyzer::ImageAnalyzer < Analyzer + def self.accept?(blob) + blob.image? + end + + def metadata + read_image do |image| + { width: image.width, height: image.height } + end + rescue LoadError + logger.info "Skipping image analysis because the mini_magick gem isn't installed" + {} + end + + private + def read_image + download_blob_to_tempfile do |file| + require "mini_magick" + yield MiniMagick::Image.new(file.path) + end + end + end +end |