https://github.com/cviebrock/image-validator
Laravel validator rules for image dimensions - Deprecated; see:
https://github.com/cviebrock/image-validator
Last synced: 3 months ago
JSON representation
Laravel validator rules for image dimensions - Deprecated; see:
- Host: GitHub
- URL: https://github.com/cviebrock/image-validator
- Owner: cviebrock
- License: mit
- Created: 2013-12-16T00:15:40.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2017-10-05T14:00:27.000Z (over 8 years ago)
- Last Synced: 2025-05-24T02:07:10.048Z (8 months ago)
- Language: PHP
- Homepage: https://laravel.com/docs/validation#rule-dimensions
- Size: 114 KB
- Stars: 223
- Watchers: 9
- Forks: 52
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-php - cviebrock/image-validator - 图片尺寸验证 (过滤和验证( Filtering ang Validation ))
README
# 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](https://laravel.com/docs/validation#rule-dimensions).
> This package is not required and will no longer be maintained.
[](https://travis-ci.org/cviebrock/image-validator)
[](https://packagist.org/packages/cviebrock/image-validator)
[](https://packagist.org/packages/cviebrock/image-validator)
[](https://packagist.org/packages/cviebrock/image-validator)
[](https://scrutinizer-ci.com/g/cviebrock/image-validator)
[](https://insight.sensiolabs.com/projects/bc2c9e90-2edf-4047-9b3c-a5aa15da165b)
[](https://opensource.org/licenses/MIT)
* [Installation](#installation)
* [Usage](#usage)
* [image_size](#image_size)
* [image_aspect](#image_aspect)
* [Examples](#examples)
* [Bugs, Suggestions and Contributions](#bugs-suggestions-and-contributions)
* [Copyright and License](#copyright-and-license)
---
## 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](https://laravel.com/docs/5.5/validation#rule-dimensions).
Install the package through [Composer](http://getcomposer.org).
```shell
composer require "cviebrock/image-validator"
```
Add the following to your `providers` array in `app/config/app.php`:
```php
'providers' => [
...
\Cviebrock\ImageValidator\ImageValidatorServiceProvider::class,
],
```
## Usage
Use it like any `Validator` rule. The package offers two rules for image validation:
### image_size
```php
$rules = [
'my_image_field' => 'image_size:[,]',
];
```
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
```php
$rules = [
'my_image_field' => 'image_aspect:',
];
```
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
```php
// 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](https://github.com/cviebrock/image-validator/graphs/contributors)
who has contributed to this project!
Please use [Github](https://github.com/cviebrock/image-validator) for reporting bugs,
and making comments or suggestions.
See [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute changes.
## Copyright and License
[image-validator](https://github.com/cviebrock/image-validator)
was written by [Colin Viebrock](http://viebrock.ca) and is released under the
[MIT License](LICENSE.md).
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.