1 | <?php namespace Cviebrock\ImageValidator; |
||
9 | class ImageValidator extends Validator |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Creates a new instance of ImageValidator |
||
14 | * |
||
15 | * @param \Illuminate\Contracts\Translation\Translator $translator |
||
16 | * @param array $data |
||
17 | * @param array $rules |
||
18 | * @param array $messages |
||
19 | * @param array $customAttributes |
||
20 | */ |
||
21 | public function __construct( |
||
22 | Translator $translator, |
||
23 | array $data, |
||
24 | array $rules, |
||
25 | array $messages = [], |
||
26 | array $customAttributes = [] |
||
27 | ) { |
||
28 | parent::__construct($translator, $data, $rules, $messages, $customAttributes); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Usage: image_size:width[,height] |
||
33 | * |
||
34 | * @param $attribute string |
||
35 | * @param $value string|array |
||
36 | * @param $parameters array |
||
37 | * @return boolean |
||
38 | */ |
||
39 | public function validateImageSize($attribute, $value, $parameters) |
||
|
|||
40 | { |
||
41 | $image = $this->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) |
||
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) |
||
231 | |||
232 | /** |
||
233 | * @param mixed $value |
||
234 | * @return string |
||
235 | */ |
||
236 | protected function getImagePath($value) |
||
251 | } |
||
252 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.