├── tests ├── .gitkeep ├── images │ ├── 1024x682.png │ ├── 1024x683.png │ └── 200x250.png ├── ValidateServiceProviderTest.php ├── ValidatorImageAspectTest.php └── ValidatorImageSizeTest.php ├── .gitignore ├── .nitpick.json ├── .semver ├── TODO.md ├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── lang ├── zh-CN │ └── validation.php ├── zh-tw │ └── validation.php ├── sr │ └── validation.php ├── nl │ └── validation.php ├── de │ └── validation.php ├── tr │ └── validation.php ├── fa │ └── validation.php ├── en │ └── validation.php ├── et │ └── validation.php ├── hu │ └── validation.php ├── pt │ └── validation.php ├── pt-BR │ └── validation.php ├── es │ └── validation.php ├── it │ └── validation.php ├── lv │ └── validation.php ├── fr │ └── validation.php └── ru │ └── validation.php ├── .travis.yml ├── .scrutinizer.yml ├── phpunit.xml ├── LICENSE.md ├── composer.json ├── CONTRIBUTING.md ├── src ├── ImageValidatorServiceProvider.php └── ImageValidator.php ├── CHANGELOG.md └── README.md /tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /vendor/ 3 | composer.lock 4 | composer.phar 5 | -------------------------------------------------------------------------------- /.nitpick.json: -------------------------------------------------------------------------------- 1 | { 2 | "ignore": [ 3 | "tests/*" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.semver: -------------------------------------------------------------------------------- 1 | --- 2 | :major: 2 3 | :minor: 2 4 | :patch: 2 5 | :special: '' 6 | :metadata: '' 7 | -------------------------------------------------------------------------------- /tests/images/1024x682.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cviebrock/image-validator/HEAD/tests/images/1024x682.png -------------------------------------------------------------------------------- /tests/images/1024x683.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cviebrock/image-validator/HEAD/tests/images/1024x683.png -------------------------------------------------------------------------------- /tests/images/200x250.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cviebrock/image-validator/HEAD/tests/images/200x250.png -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | # TODO 2 | 3 | - [ ] add more rules? 4 | - [ ] make error messages publishable via `artisan vendor:publish` 5 | - [ ] test in an actual Laravel 5 project 6 | - [ ] test validator messages / translations 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | This package has been abandoned since Laravel 5.2 and later now include built-in rules to validate images: 2 | 3 | https://laravel.com/docs/5.5/validation#rule-dimensions 4 | 5 | Issues for fixes to versions prior to Laravel 5.2 will be addressed on a case-by-case basis. 6 | 7 | **Thank you!** 8 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for helping to make this package better! 2 | 3 | However, this package has been abandoned since Laravel 5.2 and later now include built-in rules to validate images: 4 | 5 | https://laravel.com/docs/5.5/validation#rule-dimensions 6 | 7 | Pull requests are now longer being accepted. 8 | 9 | **Thank you!** 10 | -------------------------------------------------------------------------------- /lang/zh-CN/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute 的宽度为 :width, 高度为 :height', 5 | 6 | 'between' => '必须在 :size1 和 :size2 像素之间', 7 | 'lessthan' => '必须小于 :size 像素', 8 | 'lessthanorequal' => '必须小于或等于 :size 像素', 9 | 'greaterthan' => '必须大于 :size 像素', 10 | 'greaterthanorequal' => '必须大于或等于 :size 像素', 11 | 'equal' => '必须等于 :size 像素', 12 | 'anysize' => '可以为任意大小', 13 | 14 | 'image_aspect' => ':attribute 长宽比必须为 :aspect', 15 | ]; 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Travis CI configuration 2 | 3 | language: php 4 | 5 | php: 6 | - 5.6 7 | - 7.0 8 | 9 | matrix: 10 | include: 11 | - php: 5.6 12 | env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"' 13 | 14 | before_script: 15 | - travis_retry composer self-update 16 | - travis_retry composer install --prefer-source --no-interaction --dev $PREFER_LOWEST 17 | 18 | script: 19 | - php vendor/bin/phpunit 20 | 21 | after_script: 22 | - php vendor/bin/ocular code-coverage:upload --format=php-clover build/clover.xml 23 | -------------------------------------------------------------------------------- /lang/zh-tw/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute 欄位的寬度 :width, 而高度 :height', 5 | 6 | 'between' => '必須介於 :size1 與 :size2 像素之間', 7 | 'lessthan' => '必須小於 :size 像素', 8 | 'lessthanorequal' => '必須小於或等於 :size 像素', 9 | 'greaterthan' => '必須大於 :size 像素', 10 | 'greaterthanorequal' => '必須大於或等於 :size 像素', 11 | 'equal' => '必須等於 :size 像素', 12 | 'anysize' => '可以是任意大小', 13 | 14 | 'image_aspect' => ':attribute 欄位的長寬比必須是 :aspect', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/sr/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute mora biti :width u širini i :height u visini.', 5 | 'between' => 'između :size1 i :size2 px', 6 | 'lessthan' => 'manja od :size px', 7 | 'lessthanorequal' => 'manja ili jednaka :size px', 8 | 'greaterthan' => 'veća od :size px', 9 | 'greaterthanorequal' => 'veća ili jednaka :size px', 10 | 'equal' => ':size px', 11 | 'anysize' => 'bilo kojih dimenzija', 12 | 'image_aspect' => ':attribute mora da bude u proporciji :aspect.', 13 | ]; 14 | -------------------------------------------------------------------------------- /lang/nl/validation.php: -------------------------------------------------------------------------------- 1 | 'Het :attribute moet :width breed en :height hoog zijn.', 5 | 6 | 'between' => 'tussen :size1 en :size2 pixels', 7 | 'lessthan' => 'kleiner dan :size pixels', 8 | 'lessthanorequal' => 'maximaal :size pixels', 9 | 'greaterthan' => 'groter dan :size pixels', 10 | 'greaterthanorequal' => 'minimaal :size pixels', 11 | 'equal' => ':size pixels', 12 | 'anysize' => 'elke grootte', 13 | 14 | 'image_aspect' => 'De :attribute aspect ratio moet :aspect zijn.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/de/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute muss :width breit und :height hoch sein.', 5 | 6 | 'between' => 'zwischen :size1 und :size2 Pixel', 7 | 'lessthan' => 'kleiner als :size Pixel', 8 | 'lessthanorequal' => 'höchstens :size Pixel', 9 | 'greaterthan' => 'größer als :size Pixel', 10 | 'greaterthanorequal' => 'mindestens :size Pixel', 11 | 'equal' => ':size Pixel', 12 | 'anysize' => 'irgendeine Größe', 13 | 14 | 'image_aspect' => 'Das Seitenverhältnis von :attribute muss :aspect sein', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/tr/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute :width genişliği ve :height yüksekliği olmalıdır.', 5 | 6 | 'between' => ':size1 ve :size2 piksel aralığında', 7 | 'lessthan' => ':size pikselden az', 8 | 'lessthanorequal' => ':size piksele eşit veya az', 9 | 'greaterthan' => ':size pikselden fazla', 10 | 'greaterthanorequal' => ':size piksele eşit veya fazla', 11 | 'equal' => ':size piksel', 12 | 'anysize' => 'herhangi bir', 13 | 14 | 'image_aspect' => ':attribute en/boy oranı :aspect olmalıdır.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/fa/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute باید دارای طول :width و عرض :height باشد.', 5 | 6 | 'between' => 'بین :size1 و :size2 پیکسل باید باشد', 7 | 'lessthan' => 'کوچکتر از :size پیکسل', 8 | 'lessthanorequal' => 'کوچکتر یا برابر :size پیکسل', 9 | 'greaterthan' => 'بزرگتر از :size پیکسل', 10 | 'greaterthanorequal' => 'بزرگتر یا برابر :size پیکسل', 11 | 'equal' => ':size پیکسل', 12 | 'anysize' => 'هر اندازه ای', 13 | 14 | 'image_aspect' => ':attribute تناسب طول عرص برابر :aspect باید باشد.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/en/validation.php: -------------------------------------------------------------------------------- 1 | 'The :attribute must be :width wide and :height tall.', 5 | 6 | 'between' => 'between :size1 and :size2 pixels', 7 | 'lessthan' => 'less than :size pixels', 8 | 'lessthanorequal' => 'less than or equal to :size pixels', 9 | 'greaterthan' => 'greater than :size pixels', 10 | 'greaterthanorequal' => 'greater than or equal to :size pixels', 11 | 'equal' => ':size pixels', 12 | 'anysize' => 'any size', 13 | 14 | 'image_aspect' => 'The :attribute aspect ratio must be :aspect.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/et/validation.php: -------------------------------------------------------------------------------- 1 | 'Pilt peab olema :width laiust ja :height kõrgust.', 5 | 6 | 'between' => ':size1 ja :size2 piksli vahel', 7 | 'lessthan' => 'vähem, kui :size pikslit', 8 | 'lessthanorequal' => 'väiksem või võrdne :size pikslit', 9 | 'greaterthan' => 'suurem, kui :size pikslit', 10 | 'greaterthanorequal' => 'suurem või võrdne :size pikslit', 11 | 'equal' => ':size pikslit', 12 | 'anysize' => 'iga suurus', 13 | 14 | 'image_aspect' => ':attribute kuvasuhe peab olema :aspect.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/hu/validation.php: -------------------------------------------------------------------------------- 1 | 'A(z) :attribute :width szélesnek és :height magasnak kell lennie.', 5 | 6 | 'between' => ':size1 és :size2 pixel között', 7 | 'lessthan' => 'kevesebb mint :size pixel', 8 | 'lessthanorequal' => 'kevesebb vagy pont :size pixel', 9 | 'greaterthan' => 'nagyobb mint :size pixel', 10 | 'greaterthanorequal' => 'nagyobb vagy pont :size pixel', 11 | 'equal' => ':size pixel', 12 | 'anysize' => 'bármilyen méret', 13 | 14 | 'image_aspect' => 'Az) :attribute képarányának :aspect kell lennie.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/pt/validation.php: -------------------------------------------------------------------------------- 1 | 'O campo :attribute tem que ser :width de largura e :height de altura.', 5 | 6 | 'between' => 'entre :size1 e :size2 pixels', 7 | 'lessthan' => 'menor que :size pixels', 8 | 'lessthanorequal' => 'menor ou igual a :size pixels', 9 | 'greaterthan' => 'maior que :size pixels', 10 | 'greaterthanorequal' => 'maior ou igual a :size pixels', 11 | 'equal' => 'de :size pixels', 12 | 'anysize' => 'qualquer tamanho', 13 | 14 | 'image_aspect' => 'A proporção do campo :attribute tem que ser de :aspect.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/pt-BR/validation.php: -------------------------------------------------------------------------------- 1 | 'O campo :attribute tem que ser :width de largura e :height de altura.', 5 | 6 | 'between' => 'entre :size1 e :size2 pixels', 7 | 'lessthan' => 'menor que :size pixels', 8 | 'lessthanorequal' => 'menor ou igual a :size pixels', 9 | 'greaterthan' => 'maior que :size pixels', 10 | 'greaterthanorequal' => 'maior ou igual a :size pixels', 11 | 'equal' => 'de :size pixels', 12 | 'anysize' => 'qualquer tamanho', 13 | 14 | 'image_aspect' => 'A proporção do campo :attribute tem que ser de :aspect.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/es/validation.php: -------------------------------------------------------------------------------- 1 | "El campo :attribute debe ser :width de ancho y :height de alto.", 5 | 6 | 'between' => 'entre :size1 y :size2 pixeles', 7 | 'lessthan' => 'menor que :size pixeles', 8 | 'lessthanorequal' => 'menor que o equivalente a :size pixeles', 9 | 'greaterthan' => 'mayor que :size pixeles', 10 | 'greaterthanorequal' => 'mayor que o equivalente a :size pixeles', 11 | 'equal' => ':size pixeles', 12 | 'anysize' => 'cualquier tamaño', 13 | 14 | 'image_aspect' => 'El campo :attribute debe poseer un ratio de :aspect.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/it/validation.php: -------------------------------------------------------------------------------- 1 | 'Il campo :attribute dev\'essere :width in larghezza e :height in altezza.', 5 | 6 | 'between' => 'tra i :size1 e :size2 pixels', 7 | 'lessthan' => 'minore di :size pixels', 8 | 'lessthanorequal' => 'minore o uguale a :size pixels', 9 | 'greaterthan' => 'maggiore di :size pixels', 10 | 'greaterthanorequal' => 'maggiore o uguale a :size pixels', 11 | 'equal' => 'di :size pixels', 12 | 'anysize' => 'qualunque dimensione', 13 | 14 | 'image_aspect' => 'Il campo :attribute deve avere un ratio uguale a :aspect.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/lv/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute jābūt :width platam un :height augstam.', 5 | 6 | 'between' => 'starp :size1 un :size2 pikseļiem(i)', 7 | 'lessthan' => 'mazākam par :size pikseļiem(i)', 8 | 'lessthanorequal' => 'mazākam vai vienādam par :size pikseļiem(i)', 9 | 'greaterthan' => 'lielākam par :size pikseļiem(i)', 10 | 'greaterthanorequal' => 'lielākam vai vienādam par :size pikseļiem(i)', 11 | 'equal' => ':size pikseļus(i)', 12 | 'anysize' => 'jebkura izmēra', 13 | 14 | 'image_aspect' => ':attribute malu attiecībai jābūt :aspect.', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/fr/validation.php: -------------------------------------------------------------------------------- 1 | 'La taille de :attribute doit être :width de large et :height de haut', 5 | 6 | 'between' => 'entre :size1 et :size2 pixels', 7 | 'lessthan' => 'plus petite que :size pixels', 8 | 'lessthanorequal' => 'plus petite ou égale à :size pixels', 9 | 'greaterthan' => 'plus grande que :size pixels', 10 | 'greaterthanorequal' => 'plus grande ou égale à :size pixels', 11 | 'equal' => 'de :size pixels', 12 | 'anysize' => "de n'importe quelle taille", 13 | 14 | 'image_aspect' => 'Le ratio de :attribute doit être de :aspect', 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/ru/validation.php: -------------------------------------------------------------------------------- 1 | 'Изображение :attribute должно быть :width по ширине и :height по высоте.', 5 | 'between' => 'между :size1 и :size2 пикселей(я)', 6 | 'lessthan' => 'меньше, чем :size пикселей(я)', 7 | 'lessthanorequal' => 'меньше или равно :size пикселей(я)', 8 | 'greaterthan' => 'больше, чем :size пикселей(я)', 9 | 'greaterthanorequal' => 'больше или равно :size пикселей(я)', 10 | 'equal' => ':size пикселей(я)', 11 | 'anysize' => 'любого размера', 12 | 13 | 'image_aspect' => ':attribute сжатие должно быть :aspect.', 14 | ]; 15 | -------------------------------------------------------------------------------- /tests/ValidateServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | paths: ['src/*'] 3 | checks: 4 | php: 5 | code_rating: true 6 | remove_extra_empty_lines: true 7 | remove_php_closing_tag: true 8 | remove_trailing_whitespace: true 9 | fix_use_statements: 10 | remove_unused: true 11 | preserve_multiple: false 12 | preserve_blanklines: true 13 | order_alphabetically: true 14 | fix_php_opening_tag: true 15 | fix_linefeed: true 16 | fix_line_ending: true 17 | fix_identation_4spaces: true 18 | fix_doc_comments: true 19 | tools: 20 | external_code_coverage: false 21 | php_analyzer: true 22 | php_code_coverage: false 23 | php_code_sniffer: 24 | config: 25 | standard: PSR2 26 | filter: 27 | paths: ['src'] 28 | php_loc: 29 | enabled: true 30 | excluded_dirs: [vendor, tests] 31 | php_cpd: 32 | enabled: true 33 | excluded_dirs: [vendor, tests] 34 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | 20 | src/ 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Colin Viebrock 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in all 13 | > copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | > SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cviebrock/image-validator", 3 | "description": "Custom Laravel Validator for image dimensions - abandoned since these are now built in to Laravel 5.2 and later", 4 | "keywords": [ 5 | "laravel", 6 | "validator", 7 | "image", 8 | "deprecated" 9 | ], 10 | "homepage": "https://github.com/cviebrock/image-validator", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Colin Viebrock", 15 | "email": "colin@viebrock.ca" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=5.6.4", 20 | "illuminate/config": "^5.4", 21 | "illuminate/database": "^5.4", 22 | "illuminate/support": "^5.4" 23 | }, 24 | "require-dev": { 25 | "friendsofphp/php-cs-fixer": "^1.11", 26 | "mockery/mockery": "^0.9.4", 27 | "orchestra/testbench": "^3.4", 28 | "phpunit/phpunit": "~5.7", 29 | "scrutinizer/ocular": "^1.3" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Cviebrock\\ImageValidator\\": "src/" 34 | } 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "Cviebrock\\ImageValidator\\Test\\": "tests" 39 | } 40 | }, 41 | "scripts": { 42 | "test": [ 43 | "parallel-lint . --exclude vendor", 44 | "phpunit" 45 | ] 46 | }, 47 | "minimum-stability": "dev", 48 | "prefer-stable": true 49 | } 50 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via pull requests via 6 | [Github](https://github.com/cviebrock/image-validator). 7 | 8 | 1. Fork the project. 9 | 2. Create your bugfix/feature branch and write your (well-commented) code. 10 | 3. Create unit tests for your code: 11 | - Run `composer install --dev` in the root directory to install required testing packages. 12 | - Add your test classes/methods to the `/tests/` directory. 13 | - Run `vendor/bin/phpunit` and make sure everything passes (new and old tests). 14 | 3. Commit your changes (and your tests) and push to your branch. 15 | 4. Create a new pull request against this package's `master` branch. 16 | 17 | 18 | ## Pull Requests 19 | 20 | - **Use the [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md).** 21 | The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 22 | 23 | - **Add tests!** Your pull request won't be accepted if it doesn't have tests. 24 | 25 | - **Document any change in behaviour.** Make sure the `README.md` and any other relevant 26 | documentation are kept up-to-date. 27 | 28 | - **Consider our release cycle.** We try to follow [SemVer v2.0.0](http://semver.org/). 29 | Randomly breaking public APIs is not an option. 30 | 31 | - **Create feature branches.** Don't ask us to pull from your master branch. 32 | 33 | - **One pull request per feature.** If you want to do more than one thing, send multiple pull requests. 34 | 35 | - **Send coherent history.** - Make sure each individual commit in your pull request is meaningful. 36 | If you had to make multiple intermediate commits while developing, please 37 | [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) 38 | before submitting. 39 | 40 | - Don't worry about updating `CHANGELOG.md` or `.semver`. The package administrator 41 | will handle updating those when new releases are created. 42 | 43 | 44 | **Thank you!** 45 | -------------------------------------------------------------------------------- /src/ImageValidatorServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadTranslationsFrom(__DIR__ . '/../lang', 'image-validator'); 32 | 33 | $messages = trans('image-validator::validation'); 34 | 35 | $this->app->bind(ImageValidator::class, function($app) use ($messages) { 36 | $validator = new ImageValidator($app['translator'], [], [], $messages); 37 | 38 | if (isset($app['validation.presence'])) { 39 | $validator->setPresenceVerifier($app['validation.presence']); 40 | } 41 | 42 | return $validator; 43 | }); 44 | 45 | $this->addNewRules(); 46 | } 47 | 48 | /** 49 | * Get the list of new rules being added to the validator. 50 | * 51 | * @return array 52 | */ 53 | public function getRules() 54 | { 55 | return $this->rules; 56 | } 57 | 58 | /** 59 | * Add new rules to the validator. 60 | */ 61 | protected function addNewRules() 62 | { 63 | foreach ($this->getRules() as $rule) { 64 | $this->extendValidator($rule); 65 | } 66 | } 67 | 68 | /** 69 | * Extend the validator with new rules. 70 | * 71 | * @param string $rule 72 | * @return void 73 | */ 74 | protected function extendValidator($rule) 75 | { 76 | $method = studly_case($rule); 77 | $translation = $this->app['translator']->trans('image-validator::validation.' . $rule); 78 | $this->app['validator']->extend($rule, 'Cviebrock\ImageValidator\ImageValidator@validate' . $method, 79 | $translation); 80 | $this->app['validator']->replacer($rule, 'Cviebrock\ImageValidator\ImageValidator@replace' . $method); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/ValidatorImageAspectTest.php: -------------------------------------------------------------------------------- 1 | translator = Mockery::mock(Translator::class); 21 | $this->translator->shouldReceive('trans'); 22 | $this->data = [ 23 | 'image' => __DIR__ . '/images/200x250.png', 24 | ]; 25 | } 26 | 27 | public function tearDown() 28 | { 29 | Mockery::close(); 30 | } 31 | 32 | public function testValidatesAspect() 33 | { 34 | $validator = new ImageValidator( 35 | $this->translator, 36 | $this->data, 37 | ['image' => 'image_aspect:4,5'] 38 | ); 39 | 40 | $this->assertTrue($validator->passes()); 41 | } 42 | 43 | public function testValidatesAspectDecimal() 44 | { 45 | $validator = new ImageValidator( 46 | $this->translator, 47 | $this->data, 48 | ['image' => 'image_aspect:0.8'] 49 | ); 50 | 51 | $this->assertTrue($validator->passes()); 52 | } 53 | 54 | public function testValidatesReverseAspect() 55 | { 56 | $validator = new ImageValidator( 57 | $this->translator, 58 | $this->data, 59 | ['image' => 'image_aspect:~5,4'] 60 | ); 61 | 62 | $this->assertTrue($validator->passes()); 63 | } 64 | 65 | public function testValidatesReverseAspectDecimal() 66 | { 67 | $validator = new ImageValidator( 68 | $this->translator, 69 | $this->data, 70 | ['image' => 'image_aspect:~1.25'] 71 | ); 72 | 73 | $this->assertTrue($validator->passes()); 74 | } 75 | 76 | public function testRoundingAspects() 77 | { 78 | $validator = new ImageValidator( 79 | $this->translator, 80 | ['image' => __DIR__ . '/images/1024x682.png'], 81 | ['image' => 'image_aspect:3,2'] 82 | ); 83 | 84 | $this->assertFalse($validator->passes()); 85 | 86 | $validator = new ImageValidator( 87 | $this->translator, 88 | ['image' => __DIR__ . '/images/1024x683.png'], 89 | ['image' => 'image_aspect:3,2'] 90 | ); 91 | 92 | $this->assertTrue($validator->passes()); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## 2.2.2 - 04-Apr-2017 4 | 5 | - Correction to Italian (#55, thanks @neomusic) 6 | 7 | 8 | ## 2.2.1 - 08-Feb-2017 9 | 10 | - Hungarian translation (#54, thanks @GaraiViktor) 11 | 12 | 13 | ## 2.2.0 - 07-Feb-2017 14 | 15 | - Laravel 5.4 support 16 | 17 | 18 | ## 2.1.5 - 30-Jan-2017 19 | 20 | - Correction to Persian (#51, thanks @smart-twists) 21 | - Estonian translation (#52, thanks @kaidoj) 22 | 23 | 24 | ## 2.1.4 - 13-Dec-2016 25 | 26 | - Serbian (Latin) translation (#49, thanks @Shonetow) 27 | - Persian translation (#50, thanks @smart-twists) 28 | 29 | 30 | ## 2.1.3 - 12-Aug-2016 31 | 32 | - Chinese-Simplified translation (#48, thanks @php-cpm) 33 | 34 | 35 | ## 2.1.2 - 20-Jul-2016 36 | 37 | - Chinese-Traditional translation (#47, thanks @CaraWang) 38 | 39 | 40 | ## 2.1.1 - 19-Jul-2016 41 | 42 | - Fix for translations not loading in closure (#45, thanks @nWidart) 43 | 44 | 45 | ## 2.1.0 - 27-May-2016 46 | 47 | - Latvian translation (#42, thanks @scvaer) 48 | - Fix for aspect ratio checking on images where the result would be fractional pixels (#31) 49 | - Fix where aspect error message only lists one parameter when using `3,2` format for aspect (#41) 50 | - Fix for Lumen (#44, thanks @WrongWay) 51 | - Fix where translations aren't yet available (#30, thanks @superdummy) 52 | 53 | 54 | ## 2.0.1 - 11-Apr-2016 55 | 56 | - Spanish translation (#32, thanks @keyduq). 57 | - French translation fixes (#39, thanks @DevJuju). 58 | 59 | 60 | ## 2.0.0 - 07-Oct-2015 61 | 62 | - Russian translation (#29, thanks @DexinDev). 63 | 64 | 65 | ## 2.0.0-beta - 02-Mar-2015 66 | 67 | - Laravel 5 release version. 68 | - German and Dutch translations (#18 and #19, thanks @gpluess). 69 | 70 | 71 | ## 2.0.0-alpha - 11-Feb-2015 72 | 73 | - Laravel 5 alpha version. 74 | 75 | 76 | ## 1.0.5 - 11-Feb-2015 77 | 78 | - Fixed Turkish translation (#7, thanks @mayoz). 79 | - Fixed Italian translation (#55, thanks @neomusic). 80 | - Last version that works with Laravel 4. 81 | 82 | 83 | ## 1.0.4 - 17-Jun-2014 84 | 85 | - Added translation for French (merci @yazuu). 86 | 87 | 88 | ## 1.0.3 - 04-Jun-2014 89 | 90 | - Added translations for Portuguese (thanks @helio-correia) and Turkish (thanks @Ardakilic). 91 | 92 | 93 | ## 1.0.2 - 04-Feb-2014 94 | 95 | - Bug fixes. 96 | 97 | 98 | ## 1.0.0 - 04-Feb-2014 99 | 100 | - Rename package to `cviebrock/image-validator`. 101 | - Require Laravel >= 4.1.21 due to validation replacement rules. 102 | - Use better validation extension. 103 | 104 | 105 | ## 0.2.0 - 20-Dec-2013 106 | 107 | - Added `image_aspect` validation. 108 | 109 | 110 | ## 0.1.1 - 16-Dec-2013 111 | 112 | - Initial release. 113 | -------------------------------------------------------------------------------- /tests/ValidatorImageSizeTest.php: -------------------------------------------------------------------------------- 1 | translator = Mockery::mock(Translator::class); 21 | $this->translator->shouldReceive('trans'); 22 | $this->data = [ 23 | 'image' => __DIR__ . '/images/200x250.png', 24 | ]; 25 | } 26 | 27 | public function tearDown() 28 | { 29 | Mockery::close(); 30 | } 31 | 32 | public function testValidatesMatch() 33 | { 34 | $validator = new ImageValidator( 35 | $this->translator, 36 | $this->data, 37 | ['image' => 'image_size:200,250'] 38 | ); 39 | 40 | $this->assertTrue($validator->passes()); 41 | } 42 | 43 | public function testValidatesSquare() 44 | { 45 | $validator = new ImageValidator( 46 | $this->translator, 47 | $this->data, 48 | ['image' => 'image_size:200'] 49 | ); 50 | 51 | $this->assertTrue($validator->fails()); 52 | } 53 | 54 | public function testValidatesLessThan() 55 | { 56 | $validator = new ImageValidator( 57 | $this->translator, 58 | $this->data, 59 | ['image' => 'image_size:<200,<250'] 60 | ); 61 | 62 | $this->assertTrue($validator->fails()); 63 | } 64 | 65 | public function testValidatesLessThanEqual() 66 | { 67 | $validator = new ImageValidator( 68 | $this->translator, 69 | $this->data, 70 | ['image' => 'image_size:<=200,<=250'] 71 | ); 72 | 73 | $this->assertTrue($validator->passes()); 74 | } 75 | 76 | public function testValidatesGreaterThan() 77 | { 78 | $validator = new ImageValidator( 79 | $this->translator, 80 | $this->data, 81 | ['image' => 'image_size:>200,>250'] 82 | ); 83 | 84 | $this->assertTrue($validator->fails()); 85 | } 86 | 87 | public function testValidatesGreaterThanEqual() 88 | { 89 | $validator = new ImageValidator( 90 | $this->translator, 91 | $this->data, 92 | ['image' => 'image_size:>=200,>=250'] 93 | ); 94 | 95 | $this->assertTrue($validator->passes()); 96 | } 97 | 98 | public function testValidatesAnySize() 99 | { 100 | $validator = new ImageValidator( 101 | $this->translator, 102 | $this->data, 103 | ['image' => 'image_size:*,250'] 104 | ); 105 | 106 | $this->assertTrue($validator->passes()); 107 | } 108 | 109 | public function testValidatesRange() 110 | { 111 | $validator = new ImageValidator( 112 | $this->translator, 113 | $this->data, 114 | ['image' => 'image_size:200-300'] 115 | ); 116 | 117 | $this->assertTrue($validator->passes()); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image-Validator 2 | 3 | Extra validation rules for dealing with images in Laravel 5. 4 | 5 | > *NOTE*: As of Laravel version 5.2, there are now 6 | [built-in validation rules for image dimensions and aspect ratios](https://laravel.com/docs/validation#rule-dimensions). 7 | > This package is not required and will no longer be maintained. 8 | 9 | [![Build Status](https://travis-ci.org/cviebrock/image-validator.svg?branch=master&format=flat)](https://travis-ci.org/cviebrock/image-validator) 10 | [![Total Downloads](https://poser.pugx.org/cviebrock/image-validator/downloads?format=flat)](https://packagist.org/packages/cviebrock/image-validator) 11 | [![Latest Stable Version](https://poser.pugx.org/cviebrock/image-validator/v/stable?format=flat)](https://packagist.org/packages/cviebrock/image-validator) 12 | [![Latest Unstable Version](https://poser.pugx.org/cviebrock/image-validator/v/unstable?format=flat)](https://packagist.org/packages/cviebrock/image-validator) 13 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cviebrock/image-validator/badges/quality-score.png?format=flat)](https://scrutinizer-ci.com/g/cviebrock/image-validator) 14 | [![SensioLabsInsight](https://insight.sensiolabs.com/projects/bc2c9e90-2edf-4047-9b3c-a5aa15da165b/mini.png)](https://insight.sensiolabs.com/projects/bc2c9e90-2edf-4047-9b3c-a5aa15da165b) 15 | [![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg?style=flat-square)](https://opensource.org/licenses/MIT) 16 | 17 | * [Installation](#installation) 18 | * [Usage](#usage) 19 | * [image_size](#image_size) 20 | * [image_aspect](#image_aspect) 21 | * [Examples](#examples) 22 | * [Bugs, Suggestions and Contributions](#bugs-suggestions-and-contributions) 23 | * [Copyright and License](#copyright-and-license) 24 | 25 | --- 26 | 27 | ## Installation 28 | 29 | > **NOTE**: Depending on your version of Laravel, you should install a different 30 | > version of the package: 31 | > 32 | > | Laravel Version | Package Version | 33 | > |:---------------:|:---------------:| 34 | > | 4.* | 1.x | 35 | > | 5.0–5.3 | 2.1† | 36 | > | 5.4 | 2.2† | 37 | > | 5.5 | not supported† | 38 | > 39 | > † Laravel 5.2 and later have [built-in validation rules for image dimensions](https://laravel.com/docs/5.5/validation#rule-dimensions). 40 | 41 | Install the package through [Composer](http://getcomposer.org). 42 | 43 | ```shell 44 | composer require "cviebrock/image-validator" 45 | ``` 46 | 47 | Add the following to your `providers` array in `app/config/app.php`: 48 | 49 | ```php 50 | 'providers' => [ 51 | ... 52 | \Cviebrock\ImageValidator\ImageValidatorServiceProvider::class, 53 | ], 54 | ``` 55 | 56 | 57 | 58 | ## Usage 59 | 60 | Use it like any `Validator` rule. The package offers two rules for image validation: 61 | 62 | ### image_size 63 | 64 | ```php 65 | $rules = [ 66 | 'my_image_field' => 'image_size:[,]', 67 | ]; 68 | ``` 69 | 70 | The values for _width_ and _height can be integers, or integers with a modifier prefix: 71 | 72 | - `300` or `=300` means the dimension must be exactly 300 pixels. 73 | - `<300` means the dimension must be less than 300 pixels 74 | - `<=300` means the dimension must be less than or equal to 300 pixels 75 | - `>300` means the dimension must be greater than 300 pixels 76 | - `>=300` means the dimension must be greater than or equal to 300 pixels 77 | - `200-300` means the dimension must be between 200 and 300 pixels (inclusive) 78 | - `*` means the dimension can be any value 79 | 80 | If you only pass one value, it's assumed to apply to both dimensions 81 | (i.e. a square image with the given dimensions). 82 | 83 | ### image_aspect 84 | 85 | ```php 86 | $rules = [ 87 | 'my_image_field' => 'image_aspect:', 88 | ]; 89 | ``` 90 | 91 | The value for _ratio_ represents _width ÷ height_ and be either a decimal or 92 | two values (width, height; both integers): 93 | 94 | - `0.75` 95 | - `3,4` 96 | 97 | The value (or first value, if providing height and width) can also be prefixed 98 | with a tilde `~` character, in which case the orientation does not matter: 99 | 100 | - `~3,4` means the image can have an aspect ratio of either 3:4 or 4:3. 101 | 102 | Note that you may run into issues with floating point rounding. 103 | 104 | 105 | ## Examples 106 | 107 | ```php 108 | // logo must be 300px wide by 400px tall 109 | $rules = [ 110 | 'logo' => 'required|image|image_size:300,400', 111 | ]; 112 | 113 | // logo must be less than or equal to 300x300px. 114 | $rules = [ 115 | 'logo' => 'required|image|image_size:<=300', 116 | ]; 117 | 118 | // logo must be 300px wide but can be any height 119 | $rules = [ 120 | 'logo' => 'required|image|image_size:300,*', 121 | ]; 122 | 123 | // logo must be at least 100px tall and 200-300 pixels wide (inclusive) 124 | $rules = [ 125 | 'logo' => 'required|image|image_size:>=100,200-300', 126 | ]; 127 | 128 | // logo must be square 129 | $rules = [ 130 | 'logo' => 'required|image|image_aspect:1', 131 | ]; 132 | 133 | // logo must be ready for the big screen TV :) 134 | $rules = [ 135 | 'logo' => 'required|image|image_aspect:16,9', 136 | ]; 137 | ``` 138 | 139 | 140 | 141 | ## Bugs, Suggestions and Contributions 142 | 143 | Thanks to [everyone](https://github.com/cviebrock/image-validator/graphs/contributors) 144 | who has contributed to this project! 145 | 146 | Please use [Github](https://github.com/cviebrock/image-validator) for reporting bugs, 147 | and making comments or suggestions. 148 | 149 | See [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute changes. 150 | 151 | 152 | 153 | ## Copyright and License 154 | 155 | [image-validator](https://github.com/cviebrock/image-validator) 156 | was written by [Colin Viebrock](http://viebrock.ca) and is released under the 157 | [MIT License](LICENSE.md). 158 | 159 | Copyright 2013 Colin Viebrock 160 | 161 | 162 | 163 | ## Thanks 164 | 165 | Lots of thanks to https://bitbucket.org/hampel/validate-laravel for the 166 | structure of creating a package to add validator rules to Laravel, 167 | and setting up useful unit tests. 168 | -------------------------------------------------------------------------------- /src/ImageValidator.php: -------------------------------------------------------------------------------- 1 | getImagePath($value); 42 | 43 | // Get the image dimension info, or fail. 44 | 45 | $image_size = @getimagesize($image); 46 | if ($image_size === false) { 47 | return false; 48 | } 49 | 50 | // If only one dimension rule is passed, assume it applies to both height and width. 51 | 52 | if (!isset($parameters[1])) { 53 | $parameters[1] = $parameters[0]; 54 | } 55 | 56 | // Parse the parameters. Options are: 57 | // 58 | // "300" or "=300" - dimension must be exactly 300 pixels 59 | // "<300" - dimension must be less than 300 pixels 60 | // "<=300" - dimension must be less than or equal to 300 pixels 61 | // ">300" - dimension must be greater than 300 pixels 62 | // ">=300" - dimension must be greater than or equal to 300 pixels 63 | 64 | $width_check = $this->checkDimension($parameters[0], $image_size[0]); 65 | $height_check = $this->checkDimension($parameters[1], $image_size[1]); 66 | 67 | return $width_check['pass'] && $height_check['pass']; 68 | } 69 | 70 | /** 71 | * Build the error message for validation failures. 72 | * 73 | * @param string $message 74 | * @param string $attribute 75 | * @param string $rule 76 | * @param array $parameters 77 | * @return string 78 | */ 79 | public function replaceImageSize($message, $attribute, $rule, $parameters) 80 | { 81 | $width = $height = $this->checkDimension($parameters[0]); 82 | if (isset($parameters[1])) { 83 | $height = $this->checkDimension($parameters[1]); 84 | } 85 | 86 | return str_replace( 87 | [':width', ':height'], 88 | [$width['message'], $height['message']], 89 | $message 90 | ); 91 | } 92 | 93 | /** 94 | * Usage: image_aspect:ratio 95 | * 96 | * @param $attribute string 97 | * @param $value string|array 98 | * @param $parameters array 99 | * @return boolean 100 | * @throws \RuntimeException 101 | */ 102 | public function validateImageAspect($attribute, $value, $parameters) 103 | { 104 | $image = $this->getImagePath($value); 105 | 106 | // Get the image dimension info, or fail. 107 | 108 | $image_size = @getimagesize($image); 109 | if ($image_size === false) { 110 | return false; 111 | } 112 | 113 | $image_width = $image_size[0]; 114 | $image_height = $image_size[1]; 115 | 116 | // Parse the parameter(s). Options are: 117 | // 118 | // "0.75" - one param: a decimal ratio (width/height) 119 | // "3,4" - two params: width, height 120 | // 121 | // If the first value is prefixed with "~", the orientation doesn't matter, i.e.: 122 | // 123 | // "~3,4" - would accept either "3:4" or "4:3" images 124 | 125 | $both_orientations = false; 126 | 127 | if (substr($parameters[0], 0, 1) === '~') { 128 | $parameters[0] = substr($parameters[0], 1); 129 | $both_orientations = true; 130 | } 131 | 132 | if (count($parameters) === 1) { 133 | $aspect_width = $parameters[0]; 134 | $aspect_height = 1; 135 | } else { 136 | $aspect_width = (int) $parameters[0]; 137 | $aspect_height = (int) $parameters[1]; 138 | } 139 | 140 | if ($aspect_width === 0 || $aspect_height === 0) { 141 | throw new RuntimeException('Aspect is zero or infinite: ' . $parameters[0]); 142 | } 143 | 144 | $check = ($image_width * $aspect_height) / $aspect_width; 145 | 146 | if ((int) round($check) === $image_height) { 147 | return true; 148 | } 149 | 150 | if ($both_orientations) { 151 | $check = ($image_width * $aspect_width) / $aspect_height; 152 | if ((int) round($check) === $image_height) { 153 | return true; 154 | } 155 | } 156 | 157 | return false; 158 | } 159 | 160 | /** 161 | * Build the error message for validation failures. 162 | * 163 | * @param string $message 164 | * @param string $attribute 165 | * @param string $rule 166 | * @param array $parameters 167 | * @return string 168 | */ 169 | public function replaceImageAspect($message, $attribute, $rule, $parameters) 170 | { 171 | return str_replace(':aspect', implode(':', $parameters), $message); 172 | } 173 | 174 | /** 175 | * Parse the dimension rule and check if the dimension passes the rule. 176 | * 177 | * @param string $rule 178 | * @param integer $dimension 179 | * @return array 180 | * @throws \RuntimeException 181 | */ 182 | protected function checkDimension($rule, $dimension = 0) 183 | { 184 | 185 | $dimension = (int) $dimension; 186 | 187 | if ($rule === '*') { 188 | $message = $this->translator->trans('image-validator::validation.anysize'); 189 | $pass = true; 190 | } else if (preg_match('/^(\d+)\-(\d+)$/', $rule, $matches)) { 191 | $size1 = (int) $matches[1]; 192 | $size2 = (int) $matches[2]; 193 | $message = $this->translator->trans('image-validator::validation.between', compact('size1', 'size2')); 194 | $pass = ($dimension >= $size1) && ($dimension <= $size2); 195 | } else if (preg_match('/^([<=>]*)(\d+)$/', $rule, $matches)) { 196 | 197 | $size = (int) $matches[2]; 198 | 199 | switch ($matches[1]) { 200 | case '>': 201 | $message = $this->translator->trans('image-validator::validation.greaterthan', compact('size')); 202 | $pass = $dimension > $size; 203 | break; 204 | case '>=': 205 | $message = $this->translator->trans('image-validator::validation.greaterthanorequal', 206 | compact('size')); 207 | $pass = $dimension >= $size; 208 | break; 209 | case '<': 210 | $message = $this->translator->trans('image-validator::validation.lessthan', compact('size')); 211 | $pass = $dimension < $size; 212 | break; 213 | case '<=': 214 | $message = $this->translator->trans('image-validator::validation.lessthanorequal', compact('size')); 215 | $pass = $dimension <= $size; 216 | break; 217 | case '=': 218 | case '': 219 | $message = $this->translator->trans('image-validator::validation.equal', compact('size')); 220 | $pass = $dimension == $size; 221 | break; 222 | default: 223 | throw new RuntimeException('Unknown image size validation rule: ' . $rule); 224 | } 225 | } else { 226 | throw new RuntimeException('Unknown image size validation rule: ' . $rule); 227 | } 228 | 229 | return compact('message', 'pass'); 230 | } 231 | 232 | /** 233 | * @param mixed $value 234 | * @return string 235 | */ 236 | protected function getImagePath($value) 237 | { 238 | // if were passed an instance of UploadedFile, return the path 239 | if ($value instanceof UploadedFile) { 240 | return $value->getPathname(); 241 | } 242 | 243 | // if we're passed a PHP file upload array, return the "tmp_name" 244 | if (is_array($value) && array_get($value, 'tmp_name') !== null) { 245 | return $value['tmp_name']; 246 | } 247 | 248 | // fallback: we were likely passed a path already 249 | return $value; 250 | } 251 | } 252 | --------------------------------------------------------------------------------