/framework/yii/validators/ImageValidator.php

https://github.com/auaskwho/yii2 · PHP · 197 lines · 78 code · 15 blank · 104 comment · 23 complexity · fa021e4d8682f4880bebef3018f79f5e MD5 · raw file

  1. <?php
  2. /**
  3. * Image validator class file.
  4. *
  5. * @link http://www.yiiframework.com/
  6. * @copyright Copyright (c) 2008 Yii Software LLC
  7. * @license http://www.yiiframework.com/license/
  8. */
  9. namespace yii\validators;
  10. use Yii;
  11. use yii\web\UploadedFile;
  12. use yii\helpers\FileHelper;
  13. /**
  14. * ImageValidator verifies if an attribute is receiving a valid image.
  15. *
  16. * @author Taras Gudz <gudz.taras@gmail.com>
  17. * @since 2.0
  18. */
  19. class ImageValidator extends FileValidator
  20. {
  21. /**
  22. * @var string the error message used when the uploaded file is not an image.
  23. * You may use the following tokens in the message:
  24. *
  25. * - {attribute}: the attribute name
  26. * - {file}: the uploaded file name
  27. */
  28. public $notImage;
  29. /**
  30. * @var integer the minimum width in pixels.
  31. * Defaults to null, meaning no limit.
  32. * @see underWidth
  33. */
  34. public $minWidth;
  35. /**
  36. * @var integer the maximum width in pixels.
  37. * Defaults to null, meaning no limit.
  38. * @see overWidth
  39. */
  40. public $maxWidth;
  41. /**
  42. * @var integer the minimum height in pixels.
  43. * Defaults to null, meaning no limit.
  44. * @see underHeight
  45. */
  46. public $minHeight;
  47. /**
  48. * @var integer the maximum width in pixels.
  49. * Defaults to null, meaning no limit.
  50. * @see overWidth
  51. */
  52. public $maxHeight;
  53. /**
  54. * @var array|string a list of file mime types that are allowed to be uploaded.
  55. * This can be either an array or a string consisting of file mime types
  56. * separated by space or comma (e.g. "image/jpeg, image/png").
  57. * Mime type names are case-insensitive. Defaults to null, meaning all mime types
  58. * are allowed.
  59. * @see wrongMimeType
  60. */
  61. public $mimeTypes;
  62. /**
  63. * @var string the error message used when the image is under [[minWidth]].
  64. * You may use the following tokens in the message:
  65. *
  66. * - {attribute}: the attribute name
  67. * - {file}: the uploaded file name
  68. * - {limit}: the value of [[minWidth]]
  69. */
  70. public $underWidth;
  71. /**
  72. * @var string the error message used when the image is over [[maxWidth]].
  73. * You may use the following tokens in the message:
  74. *
  75. * - {attribute}: the attribute name
  76. * - {file}: the uploaded file name
  77. * - {limit}: the value of [[maxWidth]]
  78. */
  79. public $overWidth;
  80. /**
  81. * @var string the error message used when the image is under [[minHeight]].
  82. * You may use the following tokens in the message:
  83. *
  84. * - {attribute}: the attribute name
  85. * - {file}: the uploaded file name
  86. * - {limit}: the value of [[minHeight]]
  87. */
  88. public $underHeight;
  89. /**
  90. * @var string the error message used when the image is over [[maxHeight]].
  91. * You may use the following tokens in the message:
  92. *
  93. * - {attribute}: the attribute name
  94. * - {file}: the uploaded file name
  95. * - {limit}: the value of [[maxHeight]]
  96. */
  97. public $overHeight;
  98. /**
  99. * @var string the error message used when the file has an mime type
  100. * that is not listed in [[mimeTypes]].
  101. * You may use the following tokens in the message:
  102. *
  103. * - {attribute}: the attribute name
  104. * - {file}: the uploaded file name
  105. * - {mimeTypes}: the value of [[mimeTypes]]
  106. */
  107. public $wrongMimeType;
  108. /**
  109. * Initializes the validator.
  110. */
  111. public function init()
  112. {
  113. parent::init();
  114. if ($this->notImage === null) {
  115. $this->notImage = Yii::t('yii', 'The file "{file}" is not an image.');
  116. }
  117. if ($this->underWidth === null) {
  118. $this->underWidth = Yii::t('yii', 'The file "{file}" is too small. The width cannot be smaller than {limit} pixels.');
  119. }
  120. if ($this->underHeight === null) {
  121. $this->underHeight = Yii::t('yii', 'The file "{file}" is too small. The height cannot be smaller than {limit} pixels.');
  122. }
  123. if ($this->overWidth === null) {
  124. $this->overWidth = Yii::t('yii', 'The file "{file}" is too large. The width cannot be larger than {limit} pixels.');
  125. }
  126. if ($this->overHeight === null) {
  127. $this->overHeight = Yii::t('yii', 'The file "{file}" is too large. The height cannot be larger than {limit} pixels.');
  128. }
  129. if ($this->wrongMimeType === null) {
  130. $this->wrongMimeType = Yii::t('yii', 'Only files with these mimeTypes are allowed: {mimeTypes}.');
  131. }
  132. if (!is_array($this->mimeTypes)) {
  133. $this->mimeTypes = preg_split('/[\s,]+/', strtolower($this->mimeTypes), -1, PREG_SPLIT_NO_EMPTY);
  134. }
  135. }
  136. /**
  137. * Internally validates a file object.
  138. * @param \yii\base\Model $object the object being validated
  139. * @param string $attribute the attribute being validated
  140. * @param UploadedFile $file uploaded file passed to check against a set of rules
  141. */
  142. public function validateFile($object, $attribute, $file)
  143. {
  144. parent::validateFile($object, $attribute, $file);
  145. if (!$object->hasErrors($attribute)) {
  146. $this->validateImage($object, $attribute, $file);
  147. }
  148. }
  149. /**
  150. * Internally validates a file object.
  151. * @param \yii\base\Model $object the object being validated
  152. * @param string $attribute the attribute being validated
  153. * @param UploadedFile $image uploaded file passed to check against a set of rules
  154. */
  155. public function validateImage($object, $attribute, $image)
  156. {
  157. if (!empty($this->mimeTypes) && !in_array(FileHelper::getMimeType($image->tempName), $this->mimeTypes, true)) {
  158. $this->addError($object, $attribute, $this->wrongMimeType, ['file' => $image->name, 'mimeTypes' => implode(', ', $this->mimeTypes)]);
  159. }
  160. if (false === ($imageInfo = getimagesize($image->tempName))) {
  161. $this->addError($object, $attribute, $this->notImage, ['file' => $image->name]);
  162. return;
  163. }
  164. list($width, $height, $type) = $imageInfo;
  165. if ($width == 0 || $height == 0) {
  166. $this->addError($object, $attribute, $this->notImage, ['file' => $image->name]);
  167. return;
  168. }
  169. if ($this->minWidth !== null && $width < $this->minWidth) {
  170. $this->addError($object, $attribute, $this->underWidth, ['file' => $image->name, 'limit' => $this->minWidth]);
  171. }
  172. if ($this->minHeight !== null && $height < $this->minHeight) {
  173. $this->addError($object, $attribute, $this->underHeight, ['file' => $image->name, 'limit' => $this->minHeight]);
  174. }
  175. if ($this->maxWidth !== null && $width > $this->maxWidth) {
  176. $this->addError($object, $attribute, $this->overWidth, ['file' => $image->name, 'limit' => $this->maxWidth]);
  177. }
  178. if ($this->maxHeight !== null && $height > $this->maxHeight) {
  179. $this->addError($object, $attribute, $this->overHeight, ['file' => $image->name, 'limit' => $this->maxHeight]);
  180. }
  181. }
  182. }