Skip to content

Instantly share code, notes, and snippets.

@lulalala
Forked from matheusvetor/banner.rb
Last active January 14, 2019 12:58
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save lulalala/6172653 to your computer and use it in GitHub Desktop.
Save lulalala/6172653 to your computer and use it in GitHub Desktop.
Carrierwave image model validation on image dimensions/height/width.
class Banner < ActiveRecord::Base
attr_accessor :image_width, :image_height
mount_uploader :image, ImageUploader
validate :check_dimensions, :on => :create
def check_dimensions
if !image_cache.nil? && (image.width < 300 || image.height < 300)
errors.add :image, "Dimension too small."
end
end
end
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
"/uploads/missing/#{model.class.to_s.underscore}/#{version_name}.png"
end
# for image size validation
# fetching dimensions in uploader, validating it in model
attr_reader :width, :height
before :cache, :capture_size
def capture_size(file)
if version_name.blank? # Only do this once, to the original version
if file.path.nil? # file sometimes is in memory
img = ::MiniMagick::Image::read(file.file)
@width = img[:width]
@height = img[:height]
else
@width, @height = `identify -format "%wx %h" #{file.path}`.split(/x/).map{|dim| dim.to_i }
end
end
end
# resizing uploads
process :resize_to_fill => [148, 148]
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg png)
end
end
@kalashnikovisme
Copy link

@lulalala thanks!

@SuperMasterBlasterLaser

When I run recreate_versions! it thows me an error:

  ArgumentError: wrong number of arguments (2 for 0)

This happens at this line of code https://gist.github.com/lulalala/6172653#file-image_uploader-rb-L28

@guitaroff
Copy link

Hello!
Thanks @lulalala!
Where variables are applied :image_width, :image_height?

@guitaroff
Copy link

how to check work
if file.path.nil? # file sometimes is in memory
img = ::MiniMagick::Image::read(file.file)
@width = img[:width]
@height = img[:height]


@lulalala, @kalashnikovisme

@Sh1n1x
Copy link

Sh1n1x commented Mar 17, 2016

Thanks !

@jfrux
Copy link

jfrux commented Mar 16, 2018

So you have a model for every size of image? banner avatar profile etc?
You would think there would be a way we could do this by using the same class.

I'd love to be able to add a spec of an image to any model as a belongs_to and validate it all in one swoop...?
Any tips?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment