ImageValidator.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\validators;
  8. use Yii;
  9. use yii\web\UploadedFile;
  10. /**
  11. * ImageValidator verifies if an attribute is receiving a valid image.
  12. *
  13. * @author Taras Gudz <gudz.taras@gmail.com>
  14. * @since 2.0
  15. */
  16. class ImageValidator extends FileValidator
  17. {
  18. /**
  19. * @var string the error message used when the uploaded file is not an image.
  20. * You may use the following tokens in the message:
  21. *
  22. * - {attribute}: the attribute name
  23. * - {file}: the uploaded file name
  24. */
  25. public $notImage;
  26. /**
  27. * @var int the minimum width in pixels.
  28. * Defaults to null, meaning no limit.
  29. * @see underWidth for the customized message used when image width is too small.
  30. */
  31. public $minWidth;
  32. /**
  33. * @var int the maximum width in pixels.
  34. * Defaults to null, meaning no limit.
  35. * @see overWidth for the customized message used when image width is too big.
  36. */
  37. public $maxWidth;
  38. /**
  39. * @var int the minimum height in pixels.
  40. * Defaults to null, meaning no limit.
  41. * @see underHeight for the customized message used when image height is too small.
  42. */
  43. public $minHeight;
  44. /**
  45. * @var int the maximum width in pixels.
  46. * Defaults to null, meaning no limit.
  47. * @see overWidth for the customized message used when image height is too big.
  48. */
  49. public $maxHeight;
  50. /**
  51. * @var string the error message used when the image is under [[minWidth]].
  52. * You may use the following tokens in the message:
  53. *
  54. * - {attribute}: the attribute name
  55. * - {file}: the uploaded file name
  56. * - {limit}: the value of [[minWidth]]
  57. */
  58. public $underWidth;
  59. /**
  60. * @var string the error message used when the image is over [[maxWidth]].
  61. * You may use the following tokens in the message:
  62. *
  63. * - {attribute}: the attribute name
  64. * - {file}: the uploaded file name
  65. * - {limit}: the value of [[maxWidth]]
  66. */
  67. public $overWidth;
  68. /**
  69. * @var string the error message used when the image is under [[minHeight]].
  70. * You may use the following tokens in the message:
  71. *
  72. * - {attribute}: the attribute name
  73. * - {file}: the uploaded file name
  74. * - {limit}: the value of [[minHeight]]
  75. */
  76. public $underHeight;
  77. /**
  78. * @var string the error message used when the image is over [[maxHeight]].
  79. * You may use the following tokens in the message:
  80. *
  81. * - {attribute}: the attribute name
  82. * - {file}: the uploaded file name
  83. * - {limit}: the value of [[maxHeight]]
  84. */
  85. public $overHeight;
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function init()
  90. {
  91. parent::init();
  92. if ($this->notImage === null) {
  93. $this->notImage = Yii::t('yii', 'The file "{file}" is not an image.');
  94. }
  95. if ($this->underWidth === null) {
  96. $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}}.');
  97. }
  98. if ($this->underHeight === null) {
  99. $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}}.');
  100. }
  101. if ($this->overWidth === null) {
  102. $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}}.');
  103. }
  104. if ($this->overHeight === null) {
  105. $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}}.');
  106. }
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. protected function validateValue($value)
  112. {
  113. $result = parent::validateValue($value);
  114. return empty($result) ? $this->validateImage($value) : $result;
  115. }
  116. /**
  117. * Validates an image file.
  118. * @param UploadedFile $image uploaded file passed to check against a set of rules
  119. * @return array|null the error message and the parameters to be inserted into the error message.
  120. * Null should be returned if the data is valid.
  121. */
  122. protected function validateImage($image)
  123. {
  124. if (false === ($imageInfo = getimagesize($image->tempName))) {
  125. return [$this->notImage, ['file' => $image->name]];
  126. }
  127. list($width, $height) = $imageInfo;
  128. if ($width == 0 || $height == 0) {
  129. return [$this->notImage, ['file' => $image->name]];
  130. }
  131. if ($this->minWidth !== null && $width < $this->minWidth) {
  132. return [$this->underWidth, ['file' => $image->name, 'limit' => $this->minWidth]];
  133. }
  134. if ($this->minHeight !== null && $height < $this->minHeight) {
  135. return [$this->underHeight, ['file' => $image->name, 'limit' => $this->minHeight]];
  136. }
  137. if ($this->maxWidth !== null && $width > $this->maxWidth) {
  138. return [$this->overWidth, ['file' => $image->name, 'limit' => $this->maxWidth]];
  139. }
  140. if ($this->maxHeight !== null && $height > $this->maxHeight) {
  141. return [$this->overHeight, ['file' => $image->name, 'limit' => $this->maxHeight]];
  142. }
  143. return null;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function clientValidateAttribute($model, $attribute, $view)
  149. {
  150. ValidationAsset::register($view);
  151. $options = $this->getClientOptions($model, $attribute);
  152. return 'yii.validation.image(attribute, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ', deferred);';
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function getClientOptions($model, $attribute)
  158. {
  159. $options = parent::getClientOptions($model, $attribute);
  160. $label = $model->getAttributeLabel($attribute);
  161. if ($this->notImage !== null) {
  162. $options['notImage'] = $this->formatMessage($this->notImage, [
  163. 'attribute' => $label,
  164. ]);
  165. }
  166. if ($this->minWidth !== null) {
  167. $options['minWidth'] = $this->minWidth;
  168. $options['underWidth'] = $this->formatMessage($this->underWidth, [
  169. 'attribute' => $label,
  170. 'limit' => $this->minWidth,
  171. ]);
  172. }
  173. if ($this->maxWidth !== null) {
  174. $options['maxWidth'] = $this->maxWidth;
  175. $options['overWidth'] = $this->formatMessage($this->overWidth, [
  176. 'attribute' => $label,
  177. 'limit' => $this->maxWidth,
  178. ]);
  179. }
  180. if ($this->minHeight !== null) {
  181. $options['minHeight'] = $this->minHeight;
  182. $options['underHeight'] = $this->formatMessage($this->underHeight, [
  183. 'attribute' => $label,
  184. 'limit' => $this->minHeight,
  185. ]);
  186. }
  187. if ($this->maxHeight !== null) {
  188. $options['maxHeight'] = $this->maxHeight;
  189. $options['overHeight'] = $this->formatMessage($this->overHeight, [
  190. 'attribute' => $label,
  191. 'limit' => $this->maxHeight,
  192. ]);
  193. }
  194. return $options;
  195. }
  196. }