-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMImageFileValidator.php
More file actions
107 lines (94 loc) · 3.82 KB
/
MImageFileValidator.php
File metadata and controls
107 lines (94 loc) · 3.82 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Description of MImageFileValidator
*
* @author mlapko
*
*/
class MImageFileValidator
{
public static $imageProcessor = 'image';
/**
* Min width for image
* @var integer
*/
public static $minWidth;
/**
* Max width for image
* @var integer
*/
public static $maxWidth;
/**
* Min height for image
* @var integer
*/
public static $minHeight;
/**
* Max height for image
* @var integer
*/
public static $maxHeight;
/**
* @var string the error message used when the uploaded image is too large width.
* @see maxWidth
*/
public static $tooLargeWidth;
/**
* @var string the error message used when the uploaded image is too small width.
* @see minWidth
*/
public static $tooSmallWidth;
/**
* @var string the error message used when the uploaded image is too large height.
* @see maxHeight
*/
public static $tooLargeHeight;
/**
* @var string the error message used when the uploaded image is too small height.
* @see minHeight
*/
public static $tooSmallHeight;
/**
*
* @var string the error message used when the uploaded file is not image.
*/
public static $invalidImage;
/**
* Internally validates a file object.
* @param CModel $object the object being validated
* @param string $attribute the attribute being validated
* @param string $filename exists file passed to check against a set of rules
*/
public static function validate($object, $attribute, $filename)
{
$image = Yii::app()->getComponent(self::imageProcessor)->getImageHandler();
try {
$name = pathinfo($filename, PATHINFO_BASENAME);
$image->load($filename);
if (self::minWidth !== null && $image->getWidth() < self::minWidth) {
$message = self::tooSmallWidth !== null ? self::tooSmallWidth : Yii::t('mimage', 'The image "{file}" is too small. Its width cannot be smaller than {limit} px.');
self::addError($object, $attribute, $message, array('{file}' => $name, '{limit}' => self::minWidth));
}
if (self::minHeight !== null && $image->getHeight() < self::minHeight) {
$message = self::tooSmallHeight !== null ? self::tooSmallHeight : Yii::t('mimage', 'The image "{file}" is too small. Its height cannot be smaller than {limit} px.');
self::addError($object, $attribute, $message, array('{file}' => $name, '{limit}' => self::minHeight));
}
if (self::maxWidth !== null && $image->getWidth() > self::maxWidth) {
$message = self::tooLargeWidth !== null ? self::tooLargeWidth : Yii::t('mimage', 'The image "{file}" is too large. Its width cannot exceed {limit} px.');
self::addError($object, $attribute, $message, array('{file}' => $name, '{limit}' => self::maxWidth));
}
if (self::maxHeight !== null && $image->getHeight() > self::maxHeight) {
$message = self::tooLargeHeight !== null ? self::tooLargeHeight : Yii::t('mimage', 'The image "{file}" is too large. Its height cannot exceed {limit} px.');
self::addError($object, $attribute, $message, array('{file}' => $name, '{limit}' => self::maxHeight));
}
} catch (Exception $exc) {
$message = self::invalidImage !==null ? self::invalidImage : Yii::t('mimage', '{attribute} invalid image.');
self::addError($object, $attribute, $message);
}
}
public static function addError($object, $attribute, $message, $params = array())
{
$params['{attribute}'] = $object->getAttributeLabel($attribute);
$object->addError($attribute, strtr($message, $params));
}
}