ImageValidator.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. use Symfony\Component\Validator\Exception\LogicException;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  15. /**
  16. * Validates whether a value is a valid image file and is valid
  17. * against minWidth, maxWidth, minHeight and maxHeight constraints.
  18. *
  19. * @author Benjamin Dulau <benjamin.dulau@gmail.com>
  20. * @author Bernhard Schussek <bschussek@gmail.com>
  21. */
  22. class ImageValidator extends FileValidator
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function validate($value, Constraint $constraint)
  28. {
  29. if (!$constraint instanceof Image) {
  30. throw new UnexpectedTypeException($constraint, Image::class);
  31. }
  32. $violations = \count($this->context->getViolations());
  33. parent::validate($value, $constraint);
  34. $failed = \count($this->context->getViolations()) !== $violations;
  35. if ($failed || null === $value || '' === $value) {
  36. return;
  37. }
  38. if (null === $constraint->minWidth && null === $constraint->maxWidth
  39. && null === $constraint->minHeight && null === $constraint->maxHeight
  40. && null === $constraint->minPixels && null === $constraint->maxPixels
  41. && null === $constraint->minRatio && null === $constraint->maxRatio
  42. && $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait
  43. && !$constraint->detectCorrupted) {
  44. return;
  45. }
  46. $size = @getimagesize($value);
  47. if (empty($size) || (0 === $size[0]) || (0 === $size[1])) {
  48. $this->context->buildViolation($constraint->sizeNotDetectedMessage)
  49. ->setCode(Image::SIZE_NOT_DETECTED_ERROR)
  50. ->addViolation();
  51. return;
  52. }
  53. $width = $size[0];
  54. $height = $size[1];
  55. if ($constraint->minWidth) {
  56. if (!ctype_digit((string) $constraint->minWidth)) {
  57. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width.', $constraint->minWidth));
  58. }
  59. if ($width < $constraint->minWidth) {
  60. $this->context->buildViolation($constraint->minWidthMessage)
  61. ->setParameter('{{ width }}', $width)
  62. ->setParameter('{{ min_width }}', $constraint->minWidth)
  63. ->setCode(Image::TOO_NARROW_ERROR)
  64. ->addViolation();
  65. return;
  66. }
  67. }
  68. if ($constraint->maxWidth) {
  69. if (!ctype_digit((string) $constraint->maxWidth)) {
  70. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width.', $constraint->maxWidth));
  71. }
  72. if ($width > $constraint->maxWidth) {
  73. $this->context->buildViolation($constraint->maxWidthMessage)
  74. ->setParameter('{{ width }}', $width)
  75. ->setParameter('{{ max_width }}', $constraint->maxWidth)
  76. ->setCode(Image::TOO_WIDE_ERROR)
  77. ->addViolation();
  78. return;
  79. }
  80. }
  81. if ($constraint->minHeight) {
  82. if (!ctype_digit((string) $constraint->minHeight)) {
  83. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height.', $constraint->minHeight));
  84. }
  85. if ($height < $constraint->minHeight) {
  86. $this->context->buildViolation($constraint->minHeightMessage)
  87. ->setParameter('{{ height }}', $height)
  88. ->setParameter('{{ min_height }}', $constraint->minHeight)
  89. ->setCode(Image::TOO_LOW_ERROR)
  90. ->addViolation();
  91. return;
  92. }
  93. }
  94. if ($constraint->maxHeight) {
  95. if (!ctype_digit((string) $constraint->maxHeight)) {
  96. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height.', $constraint->maxHeight));
  97. }
  98. if ($height > $constraint->maxHeight) {
  99. $this->context->buildViolation($constraint->maxHeightMessage)
  100. ->setParameter('{{ height }}', $height)
  101. ->setParameter('{{ max_height }}', $constraint->maxHeight)
  102. ->setCode(Image::TOO_HIGH_ERROR)
  103. ->addViolation();
  104. }
  105. }
  106. $pixels = $width * $height;
  107. if (null !== $constraint->minPixels) {
  108. if (!ctype_digit((string) $constraint->minPixels)) {
  109. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum amount of pixels.', $constraint->minPixels));
  110. }
  111. if ($pixels < $constraint->minPixels) {
  112. $this->context->buildViolation($constraint->minPixelsMessage)
  113. ->setParameter('{{ pixels }}', $pixels)
  114. ->setParameter('{{ min_pixels }}', $constraint->minPixels)
  115. ->setParameter('{{ height }}', $height)
  116. ->setParameter('{{ width }}', $width)
  117. ->setCode(Image::TOO_FEW_PIXEL_ERROR)
  118. ->addViolation();
  119. }
  120. }
  121. if (null !== $constraint->maxPixels) {
  122. if (!ctype_digit((string) $constraint->maxPixels)) {
  123. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum amount of pixels.', $constraint->maxPixels));
  124. }
  125. if ($pixels > $constraint->maxPixels) {
  126. $this->context->buildViolation($constraint->maxPixelsMessage)
  127. ->setParameter('{{ pixels }}', $pixels)
  128. ->setParameter('{{ max_pixels }}', $constraint->maxPixels)
  129. ->setParameter('{{ height }}', $height)
  130. ->setParameter('{{ width }}', $width)
  131. ->setCode(Image::TOO_MANY_PIXEL_ERROR)
  132. ->addViolation();
  133. }
  134. }
  135. $ratio = round($width / $height, 2);
  136. if (null !== $constraint->minRatio) {
  137. if (!is_numeric((string) $constraint->minRatio)) {
  138. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum ratio.', $constraint->minRatio));
  139. }
  140. if ($ratio < $constraint->minRatio) {
  141. $this->context->buildViolation($constraint->minRatioMessage)
  142. ->setParameter('{{ ratio }}', $ratio)
  143. ->setParameter('{{ min_ratio }}', $constraint->minRatio)
  144. ->setCode(Image::RATIO_TOO_SMALL_ERROR)
  145. ->addViolation();
  146. }
  147. }
  148. if (null !== $constraint->maxRatio) {
  149. if (!is_numeric((string) $constraint->maxRatio)) {
  150. throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum ratio.', $constraint->maxRatio));
  151. }
  152. if ($ratio > $constraint->maxRatio) {
  153. $this->context->buildViolation($constraint->maxRatioMessage)
  154. ->setParameter('{{ ratio }}', $ratio)
  155. ->setParameter('{{ max_ratio }}', $constraint->maxRatio)
  156. ->setCode(Image::RATIO_TOO_BIG_ERROR)
  157. ->addViolation();
  158. }
  159. }
  160. if (!$constraint->allowSquare && $width == $height) {
  161. $this->context->buildViolation($constraint->allowSquareMessage)
  162. ->setParameter('{{ width }}', $width)
  163. ->setParameter('{{ height }}', $height)
  164. ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
  165. ->addViolation();
  166. }
  167. if (!$constraint->allowLandscape && $width > $height) {
  168. $this->context->buildViolation($constraint->allowLandscapeMessage)
  169. ->setParameter('{{ width }}', $width)
  170. ->setParameter('{{ height }}', $height)
  171. ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
  172. ->addViolation();
  173. }
  174. if (!$constraint->allowPortrait && $width < $height) {
  175. $this->context->buildViolation($constraint->allowPortraitMessage)
  176. ->setParameter('{{ width }}', $width)
  177. ->setParameter('{{ height }}', $height)
  178. ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
  179. ->addViolation();
  180. }
  181. if ($constraint->detectCorrupted) {
  182. if (!\function_exists('imagecreatefromstring')) {
  183. throw new LogicException('Corrupted images detection requires installed and enabled GD extension.');
  184. }
  185. $resource = @imagecreatefromstring(file_get_contents($value));
  186. if (false === $resource) {
  187. $this->context->buildViolation($constraint->corruptedMessage)
  188. ->setCode(Image::CORRUPTED_IMAGE_ERROR)
  189. ->addViolation();
  190. return;
  191. }
  192. imagedestroy($resource);
  193. }
  194. }
  195. }