Created
September 24, 2011 07:33
-
-
Save kirs/1239078 to your computer and use it in GitHub Desktop.
Validation of image dimensions with CarrierWave
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class AvatarUploader < 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 | |
before :cache, :capture_size_before_cache # callback, example here: http://goo.gl/9VGHI | |
def capture_size_before_cache(new_file) | |
if model.avatar_upload_width.nil? || model.avatar_upload_height.nil? | |
model.avatar_upload_width, model.avatar_upload_height = `identify -format "%wx %h" #{new_file.path}`.split(/x/).map { |dim| dim.to_i } | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
mount_uploader :avatar, AvatarUploader | |
validates :username, :presence => true | |
validate :check_avatar_dimensions | |
def check_avatar_dimensions | |
::Rails.logger.info "Avatar upload dimensions: #{self.avatar_upload_width}x#{self.avatar_upload_height}" | |
errors.add :avatar, "Dimensions of uploaded avatar should be not less than 150x150 pixels." if self.avatar_upload_width < 150 || avatar_upload_height < 150 | |
end | |
end |
Found another way https://gist.github.com/lulalala/6172653
In my case, I've validation on image for presence true. So if I submit the form without selecting any image it shown as error undefined method
<' for nil:NilClass`on lineerrors.add :avatar, "Dimensions of uploaded avatar should be not less than 150x150 pixels." if self.avatar_upload_width < 150 || avatar_upload_height < 150
. Can you guys help in this?
just in case it's still useful for anyone, I added this to the User
model:
attr_accessor :avatar_upload_width, :avatar_upload_height
validate :check_avatar_dimensions, if :uploading?
def uploading?
avatar_upload_width.present? && avatar_upload_height.present?
end
Thus, this validation will only be triggered if we are uploading a photo (and not if we are just updating the user name)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So add this to the model code please. Otherwise it can confuse some developers.