-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageValidationServiceProvider.php
81 lines (69 loc) · 2.45 KB
/
ImageValidationServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Intervention\Image\Facades\Image;
use Validator;
/**
* Image Validation Serviceprovider
*
* This Serviceprovider hosts a set of basic image validation rules to use in Laravel's
* validation service or in Requests.
*
* @author Lucas Bares <[email protected]>
* @uses Intervention\Image\ImageServiceProvider
*
*/
class ImageValidationServiceProvider extends ServiceProvider
{
/**
* Defines the validation rules
*
* @return void
*/
public function boot()
{
// Image Width
Validator::extend('width_larger', function($attribute, $value, $parameters) {
$img = Image::make($value);
return ($img->width() > $parameters[0]);
});
Validator::extend('width_equal', function($attribute, $value, $parameters) {
$img = Image::make($value);
return ($img->width() == $parameters[0]);
});
Validator::extend('width_smaller', function($attribute, $value, $parameters) {
$img = Image::make($value);
return ($img->width() < $parameters[0]);
});
// Image Height
Validator::extend('height_larger', function($attribute, $value, $parameters) {
$img = Image::make($value);
return ($img->height() > $parameters[0]);
});
Validator::extend('height', function($attribute, $value, $parameters) {
$img = Image::make($value);
return ($img->height() == $parameters[0]);
});
Validator::extend('height_smaller', function($attribute, $value, $parameters) {
$img = Image::make($value);
return ($img->height() < $parameters[0]);
});
// Image Ratio
Validator::extend('ratio_smaller', function($attribute, $value, $parameters) {
$img = Image::make($value);
return (round($img->width()/$img->height(),2) < $parameters[0]);
});
Validator::extend('ratio_equal', function($attribute, $value, $parameters) {
$img = Image::make($value);
return (round($img->width()/$img->height(),2) == $parameters[0]);
});
Validator::extend('ratio_larger', function($attribute, $value, $parameters) {
$img = Image::make($value);
return (round($img->width()/$img->height(),2) > $parameters[0]);
});
}
public function register()
{
//
}
}