module Shrine::Plugins::StoreDimensions

  1. lib/shrine/plugins/store_dimensions.rb

The store_dimensions plugin extracts and stores dimensions of the uploaded image using the fastimage gem.

plugin :store_dimensions

It adds “width” and “height” metadata values to Shrine::UploadedFile, and creates #width, #height and #dimensions reader methods.

image = uploader.upload(file)

image.metadata["width"]  #=> 300
image.metadata["height"] #=> 500
# or
image.width  #=> 300
image.height #=> 500
# or
image.dimensions #=> [300, 500]

The fastimage gem has built-in protection against image bombs. However, if for some reason it doesn't suit your needs, you can provide a custom :analyzer:

plugin :store_dimensions, analyzer: ->(io, analyzers) do
  dimensions = analyzers[:fastimage].call(io)
  dimensions || MiniMagick::Image.new(io).dimensions
end

Methods

Public Class

  1. configure

Public Class methods

configure (uploader, opts = {})
[show source]
# File lib/shrine/plugins/store_dimensions.rb, line 33
def self.configure(uploader, opts = {})
  uploader.opts[:dimensions_analyzer] = opts.fetch(:analyzer, uploader.opts.fetch(:dimensions_analyzer, :fastimage))
end