ImageValidator.php 5.87 KB
Newer Older
Gudz Taras committed
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 107 108 109 110 111 112
<?php
/**
 * Image validator class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\validators;

use Yii;
use yii\web\UploadedFile;
use yii\helpers\FileHelper;

/**
 * ImageValidator verifies if an attribute is receiving a valid image.
 *
 * @author Taras Gudz <gudz.taras@gmail.com>
 * @since 2.0
 */
class ImageValidator extends FileValidator
{
	/**
	 * @var string the error message used when the uploaded file is not an image.
	 * You may use the following tokens in the message:
	 *
	 * - {attribute}: the attribute name
	 * - {file}: the uploaded file name
	 */
	public $notImage;
	/**
	 * @var integer the minimum width in pixels.
	 * Defaults to null, meaning no limit.
	 * @see underWidth
	 */
	public $minWidth;
	/**
	 * @var integer the maximum width in pixels.
	 * Defaults to null, meaning no limit.
	 * @see overWidth
	 */
	public $maxWidth;
	/**
	 * @var integer the minimum height in pixels.
	 * Defaults to null, meaning no limit.
	 * @see underHeight
	 */
	public $minHeight;
	/**
	 * @var integer the maximum width in pixels.
	 * Defaults to null, meaning no limit.
	 * @see overWidth
	 */
	public $maxHeight;
	/**
	 * @var array|string a list of file mime types that are allowed to be uploaded.
	 * This can be either an array or a string consisting of file mime types
	 * separated by space or comma (e.g. "image/jpeg, image/png").
	 * Mime type names are case-insensitive. Defaults to null, meaning all mime types
	 * are allowed.
	 * @see wrongMimeType
	 */
	public $mimeTypes;
	/**
	 * @var string the error message used when the image is under [[minWidth]].
	 * You may use the following tokens in the message:
	 *
	 * - {attribute}: the attribute name
	 * - {file}: the uploaded file name
	 * - {limit}: the value of [[minWidth]]
	 */
	public $underWidth;
	/**
	 * @var string the error message used when the image is over [[maxWidth]].
	 * You may use the following tokens in the message:
	 *
	 * - {attribute}: the attribute name
	 * - {file}: the uploaded file name
	 * - {limit}: the value of [[maxWidth]]
	 */
	public $overWidth;
	/**
	 * @var string the error message used when the image is under [[minHeight]].
	 * You may use the following tokens in the message:
	 *
	 * - {attribute}: the attribute name
	 * - {file}: the uploaded file name
	 * - {limit}: the value of [[minHeight]]
	 */
	public $underHeight;
	/**
	 * @var string the error message used when the image is over [[maxHeight]].
	 * You may use the following tokens in the message:
	 *
	 * - {attribute}: the attribute name
	 * - {file}: the uploaded file name
	 * - {limit}: the value of [[maxHeight]]
	 */
	public $overHeight;
	/**
	 * @var string the error message used when the file has an mime type
	 * that is not listed in [[mimeTypes]].
	 * You may use the following tokens in the message:
	 *
	 * - {attribute}: the attribute name
	 * - {file}: the uploaded file name
	 * - {mimeTypes}: the value of [[mimeTypes]]
	 */
	public $wrongMimeType;

	/**
Qiang Xue committed
113
	 * @inheritdoc
Gudz Taras committed
114 115 116 117 118 119 120 121 122
	 */
	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) {
123
			$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}}.');
Gudz Taras committed
124 125
		}
		if ($this->underHeight === null) {
126
			$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}}.');
Gudz Taras committed
127 128
		}		
		if ($this->overWidth === null) {
129
			$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}}.');
Gudz Taras committed
130 131
		}
		if ($this->overHeight === null) {
132
			$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}}.');
Gudz Taras committed
133 134 135 136 137 138 139 140 141 142
		}
		if ($this->wrongMimeType === null) {
			$this->wrongMimeType = Yii::t('yii', 'Only files with these mimeTypes are allowed: {mimeTypes}.');
		}
		if (!is_array($this->mimeTypes)) {
			$this->mimeTypes = preg_split('/[\s,]+/', strtolower($this->mimeTypes), -1, PREG_SPLIT_NO_EMPTY);
		}
	}

	/**
Qiang Xue committed
143
	 * @inheritdoc
Gudz Taras committed
144
	 */
Qiang Xue committed
145
	protected function validateValue($file)
Gudz Taras committed
146
	{
Qiang Xue committed
147 148
		$result = parent::validateValue($file);
		return empty($result) ? $this->validateImage($file) : $result;
Gudz Taras committed
149 150 151
	}
	
	/**
Qiang Xue committed
152
	 * Validates an image file.
Gudz Taras committed
153
	 * @param UploadedFile $image uploaded file passed to check against a set of rules
Qiang Xue committed
154 155
	 * @return array|null the error message and the parameters to be inserted into the error message.
	 * Null should be returned if the data is valid.
Gudz Taras committed
156
	 */
Qiang Xue committed
157
	protected function validateImage($image)
Gudz Taras committed
158 159
	{
		if (!empty($this->mimeTypes) && !in_array(FileHelper::getMimeType($image->tempName), $this->mimeTypes, true)) {
Qiang Xue committed
160
			return [$this->wrongMimeType, ['file' => $image->name, 'mimeTypes' => implode(', ', $this->mimeTypes)]];
Gudz Taras committed
161 162 163
		}
		
		if (false === ($imageInfo = getimagesize($image->tempName))) {
Qiang Xue committed
164
			return [$this->notImage, ['file' => $image->name]];
Gudz Taras committed
165 166 167 168 169
		}
		
		list($width, $height, $type) = $imageInfo;
		
		if ($width == 0 || $height == 0) {
Qiang Xue committed
170
			return [$this->notImage, ['file' => $image->name]];
Gudz Taras committed
171 172 173
		}
		
		if ($this->minWidth !== null && $width < $this->minWidth) {
Qiang Xue committed
174
			return [$this->underWidth, ['file' => $image->name, 'limit' => $this->minWidth]];
Gudz Taras committed
175 176 177
		}
		
		if ($this->minHeight !== null && $height < $this->minHeight) {
Qiang Xue committed
178
			return [$this->underHeight, ['file' => $image->name, 'limit' => $this->minHeight]];
Gudz Taras committed
179 180 181
		}
		
		if ($this->maxWidth !== null && $width > $this->maxWidth) {
Qiang Xue committed
182
			return [$this->overWidth, ['file' => $image->name, 'limit' => $this->maxWidth]];
Gudz Taras committed
183 184 185
		}
		
		if ($this->maxHeight !== null && $height > $this->maxHeight) {
Qiang Xue committed
186
			return [$this->overHeight, ['file' => $image->name, 'limit' => $this->maxHeight]];
Gudz Taras committed
187
		}
Qiang Xue committed
188
		return null;
Gudz Taras committed
189 190
	}
}