cviebrock/image-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cviebrock/image-validator

最新稳定版本:2.2.3

Composer 安装命令:

composer require cviebrock/image-validator

包简介

Custom Laravel Validator for image dimensions - abandoned since these are now built in to Laravel 5.2 and later

README 文档

README

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.

统计信息

  • 总下载量: 456.65k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 228
  • 点击次数: 1
  • 依赖项目数: 7
  • 推荐数: 3

GitHub 信息

  • Stars: 224
  • Watchers: 8
  • Forks: 53
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-04