|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
-
-
- namespace yii\validators;
-
- use Yii;
- use yii\web\UploadedFile;
-
-
- class ImageValidator extends FileValidator
- {
-
-
- public $notImage;
-
-
- public $minWidth;
-
-
- public $maxWidth;
-
-
- public $minHeight;
-
-
- public $maxHeight;
-
-
- public $underWidth;
-
-
- public $overWidth;
-
-
- public $underHeight;
-
-
- public $overHeight;
-
-
-
-
- public function init()
- {
- parent::init();
-
- if ($this->notImage === null) {
- $this->notImage = Yii::t('yii', 'The file "{file}" is not an image.');
- }
- if ($this->underWidth === null) {
- $this->underWidth = Yii::t('yii', 'The image "{file}" is too small. The width cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
- }
- if ($this->underHeight === null) {
- $this->underHeight = Yii::t('yii', 'The image "{file}" is too small. The height cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
- }
- if ($this->overWidth === null) {
- $this->overWidth = Yii::t('yii', 'The image "{file}" is too large. The width cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
- }
- if ($this->overHeight === null) {
- $this->overHeight = Yii::t('yii', 'The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
- }
- }
-
-
-
- protected function validateValue($file)
- {
- $result = parent::validateValue($file);
-
- return empty($result) ? $this->validateImage($file) : $result;
- }
-
-
-
- protected function validateImage($image)
- {
- if (false === ($imageInfo = getimagesize($image->tempName))) {
- return [$this->notImage, ['file' => $image->name]];
- }
-
- list($width, $height) = $imageInfo;
-
- if ($width == 0 || $height == 0) {
- return [$this->notImage, ['file' => $image->name]];
- }
-
- if ($this->minWidth !== null && $width < $this->minWidth) {
- return [$this->underWidth, ['file' => $image->name, 'limit' => $this->minWidth]];
- }
-
- if ($this->minHeight !== null && $height < $this->minHeight) {
- return [$this->underHeight, ['file' => $image->name, 'limit' => $this->minHeight]];
- }
-
- if ($this->maxWidth !== null && $width > $this->maxWidth) {
- return [$this->overWidth, ['file' => $image->name, 'limit' => $this->maxWidth]];
- }
-
- if ($this->maxHeight !== null && $height > $this->maxHeight) {
- return [$this->overHeight, ['file' => $image->name, 'limit' => $this->maxHeight]];
- }
-
- return null;
- }
-
-
-
- public function clientValidateAttribute($model, $attribute, $view)
- {
- ValidationAsset::register($view);
- $options = $this->getClientOptions($model, $attribute);
- return 'yii.validation.image(attribute, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ', deferred);';
- }
-
-
-
- protected function getClientOptions($model, $attribute)
- {
- $options = parent::getClientOptions($model, $attribute);
-
- $label = $model->getAttributeLabel($attribute);
-
- if ($this->notImage !== null) {
- $options['notImage'] = Yii::$app->getI18n()->format($this->notImage, [
- 'attribute' => $label,
- ], Yii::$app->language);
- }
-
- if ($this->minWidth !== null) {
- $options['minWidth'] = $this->minWidth;
- $options['underWidth'] = Yii::$app->getI18n()->format($this->underWidth, [
- 'attribute' => $label,
- 'limit' => $this->minWidth,
- ], Yii::$app->language);
- }
-
- if ($this->maxWidth !== null) {
- $options['maxWidth'] = $this->maxWidth;
- $options['overWidth'] = Yii::$app->getI18n()->format($this->overWidth, [
- 'attribute' => $label,
- 'limit' => $this->maxWidth,
- ], Yii::$app->language);
- }
-
- if ($this->minHeight !== null) {
- $options['minHeight'] = $this->minHeight;
- $options['underHeight'] = Yii::$app->getI18n()->format($this->underHeight, [
- 'attribute' => $label,
- 'limit' => $this->minHeight,
- ], Yii::$app->language);
- }
-
- if ($this->maxHeight !== null) {
- $options['maxHeight'] = $this->maxHeight;
- $options['overHeight'] = Yii::$app->getI18n()->format($this->overHeight, [
- 'attribute' => $label,
- 'limit' => $this->maxHeight,
- ], Yii::$app->language);
- }
-
- return $options;
- }
- }
|