mercredi 30 janvier 2019

Dragonfly - Image Width, Height Validation

In an old Rails application with Dragonfly for image uploads. In short, I would like to validate the width and height of the image to be greater than 1024 x 1024.

Looking in to the documentation I see there are 2 types of operations in: and as:.

I would like to implement validations for

  • width > 1024
  • Height > 1024

What I have so far is as...

# config/initializers/dragonfly.rb
require 'dragonfly'

app = Dragonfly[:images]
app.configure_with(:imagemagick)
app.configure_with(:rails)

app.define_macro(ActiveRecord::Base, :image_accessor)

and the modal

class Tenant < ActiveRecord::Base
  image_accessor :splash_image

  validates_size_of :splash_image, maximum: 10.megabyte, message: 'is too large (10 MB maximum)'
  validates_property :width, of: :splash_image, in: (1024...Float::Infinity), message: "image should be at least 1024px wide"
end

Thank you upfront.

Aucun commentaire:

Enregistrer un commentaire