Search code examples
ruby-on-railscarrierwaveminimagick

Rails carrierwave and minimagick allow landscape image only


Is it way to validate image orientation on image upload? I want that user can upload only landscape image.

Thanks


Solution

  • mount_uploader :photo, PhotoUploader
    
    validate :check_landscape
    
    def check_landscape
      if photo.width<photo.height
         errors.add :photo, "is not a landscape." 
         puts "Error ! not a Landscape Image"
      else if photo.width>photo.height
         puts " Landscape Image"
      end
      end
    end
    

    if you are looking for active_storage has_many_attached

    has_many_attached :images
    
    
    validate: active_storage_many_images
    
     def active_storage_many_images
        images.each do |image|
    
        image.blob.analyze unless image.blob.analyzed?
        width = image.blob.metadata[:width]
        height = image.blob.metadata[:height]
    
        if width<height
          errors.add :image, "Additional images are not landscape"
          puts "ACTIVE STORAGE IMAGE ERROR !!"
        end
      end
     end