Question

I need to check if the images people upload to my site have the exact width of 200px and height of 300px. If they haven't got those dimensions, they shouldn't be saved in the database.

I've tried for ages with mini-magick and gone through numerous tutorials, posts on Stack Overflow, etc. but I can't find a functional way of doing this.

I'm using ruby 1.9.3 and rails 3.2.2, on a mongoDB running mongoid.

Was really hoping you could put me in the right direction.

My image model look like this:

class Flow
    include Mongoid::Document
    include Mongoid::Taggable
    include Mongoid::Timestamps
    include Mongo::Voteable
    voteable self, :up => +1, :down => -1

    attr_accessible :image, :remote_image_url, :tags

    mount_uploader :image, UserUploader

    belongs_to :user

    field :shot, :type => String
    field :remote_image_url, :type => String
    field :tags, type: Array
end

And my carrierwave model looks like this:

class UserUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick

    storage :fog

    def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end

    def extension_white_list
        %w(jpg jpeg png gif)
    end
end
Was it helpful?

Solution

I ended up doing some RMagick in the UserUploader and the User class:

UserUploader:

process :get_geometry
def geometry
    @geometry
end

def get_geometry
    if (@file)
        img = ::Magick::Image::read(@file.file).first
        @geometry = [ img.columns, img.rows ]
    end
end

User class

validate :validate_minimum_image_size

def validate_minimum_image_size
    geometry = self.image.geometry
    if (! geometry.nil?)
        self.width = geometry[0]
        self.height = geometry[1]
    end
    unless (self.width == 320 && self.height == 480) || (self.width == 640 && self.height == 960)
        errors.add :base, "Naughty you... iPhone designs should be 320px x 480px or 640px x 960px." 
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top