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, which has built-in protection agains image bombs.

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]

You can provide your own custom dimensions analyzer, and reuse any of the built-in analyzers; you just need to return a two-element array of width and height, or nil to signal that dimensions weren't extracted.

require "mini_magick"

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

You can also use methods for extracting the dimensions directly:

# or YourUploader.extract_dimensions(io)
Shrine.extract_dimensions(io) # calls the defined analyzer
#=> [300, 400]

# or YourUploader.dimensions_analyzers
Shrine.dimensions_analyzers[:fastimage].call(io) # calls a built-in analyzer
#=> [300, 400]


Public Class

  1. configure

Public Class methods

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