• Stars
    star
    226
  • Rank 176,514 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 11 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Laravel validator rules for image dimensions - Deprecated; see:

Image-Validator

Extra validation rules for dealing with images in Laravel 5.

NOTE: As of Laravel version 5.2, there are now built-in validation rules for image dimensions and aspect ratios. This package is not required and will no longer be maintained.

Build Status Total Downloads Latest Stable Version Latest Unstable Version Scrutinizer Code Quality SensioLabsInsight License: MIT


Installation

NOTE: Depending on your version of Laravel, you should install a different version of the package:

Laravel Version Package Version
4.* 1.x
5.0–5.3 2.1†
5.4 2.2†
5.5 not supported†

† Laravel 5.2 and later have built-in validation rules for image dimensions.

Install the package through Composer.

composer require "cviebrock/image-validator"

Add the following to your providers array in app/config/app.php:

'providers' => [
    ...
    \Cviebrock\ImageValidator\ImageValidatorServiceProvider::class,
],

Usage

Use it like any Validator rule. The package offers two rules for image validation:

image_size

$rules = [
    'my_image_field' => 'image_size:<width>[,<height>]',
];

The values for width and _height can be integers, or integers with a modifier prefix:

  • 300 or =300 means the dimension must be exactly 300 pixels.
  • <300 means the dimension must be less than 300 pixels
  • <=300 means the dimension must be less than or equal to 300 pixels
  • >300 means the dimension must be greater than 300 pixels
  • >=300 means the dimension must be greater than or equal to 300 pixels
  • 200-300 means the dimension must be between 200 and 300 pixels (inclusive)
  • * means the dimension can be any value

If you only pass one value, it's assumed to apply to both dimensions (i.e. a square image with the given dimensions).

image_aspect

$rules = [
    'my_image_field' => 'image_aspect:<ratio>',
];

The value for ratio represents width ÷ height and be either a decimal or two values (width, height; both integers):

  • 0.75
  • 3,4

The value (or first value, if providing height and width) can also be prefixed with a tilde ~ character, in which case the orientation does not matter:

  • ~3,4 means the image can have an aspect ratio of either 3:4 or 4:3.

Note that you may run into issues with floating point rounding.

Examples

// logo must be 300px wide by 400px tall
$rules = [
    'logo' => 'required|image|image_size:300,400',
];

// logo must be less than or equal to 300x300px.
$rules = [
    'logo' => 'required|image|image_size:<=300',
];

// logo must be 300px wide but can be any height
$rules = [
    'logo' => 'required|image|image_size:300,*',
];

// logo must be at least 100px tall and 200-300 pixels wide (inclusive)
$rules = [
    'logo' => 'required|image|image_size:>=100,200-300',
];

// logo must be square
$rules = [
    'logo' => 'required|image|image_aspect:1',
];

// logo must be ready for the big screen TV :)
$rules = [
    'logo' => 'required|image|image_aspect:16,9',
];

Bugs, Suggestions and Contributions

Thanks to everyone who has contributed to this project!

Please use Github for reporting bugs, and making comments or suggestions.

See CONTRIBUTING.md for how to contribute changes.

Copyright and License

image-validator was written by Colin Viebrock and is released under the MIT License.

Copyright 2013 Colin Viebrock

Thanks

Lots of thanks to https://bitbucket.org/hampel/validate-laravel for the structure of creating a package to add validator rules to Laravel, and setting up useful unit tests.

More Repositories

1

eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel
PHP
3,766
star
2

sequel-pro-laravel-export

A Sequel Pro / Sequel Ace bundle to generate Laravel migration files from existing tables.
PHP
926
star
3

eloquent-taggable

Easily add the ability to tag your Eloquent models in Laravel.
PHP
468
star
4

laravel5-package-template

Boilerplate template for Laravel 5 packages
PHP
111
star
5

thumbnailable

Laravel bundle for easy thumbnail handling in Eloquent models, inspired by sfDoctrineThumbnailablePlugin
PHP
35
star
6

eloquent-typecast

Trait for Eloquent models to force type-casting on retrieved values
PHP
23
star
7

laravel-mangopay

Easily integrate the official Mangopay SDK into your Laravel/Lumen applications.
PHP
19
star
8

phpmailer-laravel

PHPMailer bundle for Laravel 3
PHP
17
star
9

VirtualBox-Menulet

Easily launch VirtualBox® machines from your macOS Menubar.
Objective-C
11
star
10

laravel-101-slides

Laravel 101 presentation for the Winnipeg PHP Meetup group
JavaScript
11
star
11

artisan-hash

Adds an Artisan task to Laravel to generate password hashes from the CLI
PHP
9
star
12

gifdown.io

gifdown.io source code
JavaScript
9
star
13

eloquent-loggable

Easy way to track changes to your Eloquent models in Laravel 5.
PHP
7
star
14

php-qrencode

PHP qrencode extension, using libqrencode
7
star
15

eloquent-log-lazy-loading

Quick way to log (or disable) lazy loading relations on Eloquent models.
PHP
6
star
16

opensrs-php

A PHP-based client class that communicates with the TuCows OpenSRS Domain Name Registry System via an API.
PHP
5
star
17

guzzle-stripbom-plugin

Plug-in for Guzzle that strips BOMs from server responses
PHP
5
star
18

laravel-news-sitemap

A Google News sitemap generator for Laravel 4.
PHP
5
star
19

laravel-101

Laravel 101 example project for the Winnipeg PHP Meetup group
PHP
4
star
20

laravel-flasher

Simple message flasher for the Laravel framework
PHP
3
star
21

laravel-elasticsearch

UNMAINTAINED - An easy way to use the official Elastic Search client in your Laravel or Lumen applications
PHP
3
star
22

pastes

The Laravel paste bucket.
PHP
2
star
23

goalfeed

PHP
2
star
24

sandbox

Just some snippets of code
2
star
25

laravel-elasticsearch-handlers

Further easiness when using ES with Laravel
PHP
2
star
26

foundation-filestyle

Better looking HTML file inputs for Zurb Foundation 5
JavaScript
2
star
27

pixie

A simple, on-the-fly image resizing server written in NodeJS.
JavaScript
2
star
28

sluggable

Laravel bundle for easy creation of slugs for your Eloquent models.
PHP
1
star
29

twig-group

Twig filter that splits an array into a given number of groups.
PHP
1
star
30

ScrabbleClub

A website framework to help track players, games and ratings for a Scrabble® club.
PHP
1
star
31

wordlists

Word List generator for games such as Scrabble
PHP
1
star
32

jTextile

A port of the Textile markup system to jQuery
PHP
1
star
33

jquery-equalheights

Equalize heights of containers via jQuery, with additional options
JavaScript
1
star
34

laravel-resources

PHP
1
star
35

twig-natural-join

Twig filter that joins an array using a different separator for the last element (e.g. "John, Mary, Bob and Jim")
PHP
1
star
36

eloquent-replaceable

PHP
1
star