Image.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /**
  12. * @Annotation
  13. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  14. *
  15. * @author Benjamin Dulau <benjamin.dulau@gmail.com>
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  19. class Image extends File
  20. {
  21. public const SIZE_NOT_DETECTED_ERROR = '6d55c3f4-e58e-4fe3-91ee-74b492199956';
  22. public const TOO_WIDE_ERROR = '7f87163d-878f-47f5-99ba-a8eb723a1ab2';
  23. public const TOO_NARROW_ERROR = '9afbd561-4f90-4a27-be62-1780fc43604a';
  24. public const TOO_HIGH_ERROR = '7efae81c-4877-47ba-aa65-d01ccb0d4645';
  25. public const TOO_LOW_ERROR = 'aef0cb6a-c07f-4894-bc08-1781420d7b4c';
  26. public const TOO_FEW_PIXEL_ERROR = '1b06b97d-ae48-474e-978f-038a74854c43';
  27. public const TOO_MANY_PIXEL_ERROR = 'ee0804e8-44db-4eac-9775-be91aaf72ce1';
  28. public const RATIO_TOO_BIG_ERROR = '70cafca6-168f-41c9-8c8c-4e47a52be643';
  29. public const RATIO_TOO_SMALL_ERROR = '59b8c6ef-bcf2-4ceb-afff-4642ed92f12e';
  30. public const SQUARE_NOT_ALLOWED_ERROR = '5d41425b-facb-47f7-a55a-de9fbe45cb46';
  31. public const LANDSCAPE_NOT_ALLOWED_ERROR = '6f895685-7cf2-4d65-b3da-9029c5581d88';
  32. public const PORTRAIT_NOT_ALLOWED_ERROR = '65608156-77da-4c79-a88c-02ef6d18c782';
  33. public const CORRUPTED_IMAGE_ERROR = '5d4163f3-648f-4e39-87fd-cc5ea7aad2d1';
  34. // Include the mapping from the base class
  35. protected static $errorNames = [
  36. self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
  37. self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
  38. self::EMPTY_ERROR => 'EMPTY_ERROR',
  39. self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
  40. self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
  41. self::SIZE_NOT_DETECTED_ERROR => 'SIZE_NOT_DETECTED_ERROR',
  42. self::TOO_WIDE_ERROR => 'TOO_WIDE_ERROR',
  43. self::TOO_NARROW_ERROR => 'TOO_NARROW_ERROR',
  44. self::TOO_HIGH_ERROR => 'TOO_HIGH_ERROR',
  45. self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
  46. self::TOO_FEW_PIXEL_ERROR => 'TOO_FEW_PIXEL_ERROR',
  47. self::TOO_MANY_PIXEL_ERROR => 'TOO_MANY_PIXEL_ERROR',
  48. self::RATIO_TOO_BIG_ERROR => 'RATIO_TOO_BIG_ERROR',
  49. self::RATIO_TOO_SMALL_ERROR => 'RATIO_TOO_SMALL_ERROR',
  50. self::SQUARE_NOT_ALLOWED_ERROR => 'SQUARE_NOT_ALLOWED_ERROR',
  51. self::LANDSCAPE_NOT_ALLOWED_ERROR => 'LANDSCAPE_NOT_ALLOWED_ERROR',
  52. self::PORTRAIT_NOT_ALLOWED_ERROR => 'PORTRAIT_NOT_ALLOWED_ERROR',
  53. self::CORRUPTED_IMAGE_ERROR => 'CORRUPTED_IMAGE_ERROR',
  54. ];
  55. public $mimeTypes = 'image/*';
  56. public $minWidth;
  57. public $maxWidth;
  58. public $maxHeight;
  59. public $minHeight;
  60. public $maxRatio;
  61. public $minRatio;
  62. public $minPixels;
  63. public $maxPixels;
  64. public $allowSquare = true;
  65. public $allowLandscape = true;
  66. public $allowPortrait = true;
  67. public $detectCorrupted = false;
  68. // The constant for a wrong MIME type is taken from the parent class.
  69. public $mimeTypesMessage = 'This file is not a valid image.';
  70. public $sizeNotDetectedMessage = 'The size of the image could not be detected.';
  71. public $maxWidthMessage = 'The image width is too big ({{ width }}px). Allowed maximum width is {{ max_width }}px.';
  72. public $minWidthMessage = 'The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px.';
  73. public $maxHeightMessage = 'The image height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px.';
  74. public $minHeightMessage = 'The image height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px.';
  75. public $minPixelsMessage = 'The image has too few pixels ({{ pixels }} pixels). Minimum amount expected is {{ min_pixels }} pixels.';
  76. public $maxPixelsMessage = 'The image has too many pixels ({{ pixels }} pixels). Maximum amount expected is {{ max_pixels }} pixels.';
  77. public $maxRatioMessage = 'The image ratio is too big ({{ ratio }}). Allowed maximum ratio is {{ max_ratio }}.';
  78. public $minRatioMessage = 'The image ratio is too small ({{ ratio }}). Minimum ratio expected is {{ min_ratio }}.';
  79. public $allowSquareMessage = 'The image is square ({{ width }}x{{ height }}px). Square images are not allowed.';
  80. public $allowLandscapeMessage = 'The image is landscape oriented ({{ width }}x{{ height }}px). Landscape oriented images are not allowed.';
  81. public $allowPortraitMessage = 'The image is portrait oriented ({{ width }}x{{ height }}px). Portrait oriented images are not allowed.';
  82. public $corruptedMessage = 'The image file is corrupted.';
  83. /**
  84. * {@inheritdoc}
  85. *
  86. * @param int|float $maxRatio
  87. * @param int|float $minRatio
  88. * @param int|float $minPixels
  89. * @param int|float $maxPixels
  90. */
  91. public function __construct(
  92. array $options = null,
  93. $maxSize = null,
  94. bool $binaryFormat = null,
  95. array $mimeTypes = null,
  96. int $minWidth = null,
  97. int $maxWidth = null,
  98. int $maxHeight = null,
  99. int $minHeight = null,
  100. $maxRatio = null,
  101. $minRatio = null,
  102. $minPixels = null,
  103. $maxPixels = null,
  104. bool $allowSquare = null,
  105. bool $allowLandscape = null,
  106. bool $allowPortrait = null,
  107. bool $detectCorrupted = null,
  108. string $notFoundMessage = null,
  109. string $notReadableMessage = null,
  110. string $maxSizeMessage = null,
  111. string $mimeTypesMessage = null,
  112. string $disallowEmptyMessage = null,
  113. string $uploadIniSizeErrorMessage = null,
  114. string $uploadFormSizeErrorMessage = null,
  115. string $uploadPartialErrorMessage = null,
  116. string $uploadNoFileErrorMessage = null,
  117. string $uploadNoTmpDirErrorMessage = null,
  118. string $uploadCantWriteErrorMessage = null,
  119. string $uploadExtensionErrorMessage = null,
  120. string $uploadErrorMessage = null,
  121. string $sizeNotDetectedMessage = null,
  122. string $maxWidthMessage = null,
  123. string $minWidthMessage = null,
  124. string $maxHeightMessage = null,
  125. string $minHeightMessage = null,
  126. string $minPixelsMessage = null,
  127. string $maxPixelsMessage = null,
  128. string $maxRatioMessage = null,
  129. string $minRatioMessage = null,
  130. string $allowSquareMessage = null,
  131. string $allowLandscapeMessage = null,
  132. string $allowPortraitMessage = null,
  133. string $corruptedMessage = null,
  134. array $groups = null,
  135. $payload = null
  136. ) {
  137. parent::__construct(
  138. $options,
  139. $maxSize,
  140. $binaryFormat,
  141. $mimeTypes,
  142. $notFoundMessage,
  143. $notReadableMessage,
  144. $maxSizeMessage,
  145. $mimeTypesMessage,
  146. $disallowEmptyMessage,
  147. $uploadIniSizeErrorMessage,
  148. $uploadFormSizeErrorMessage,
  149. $uploadPartialErrorMessage,
  150. $uploadNoFileErrorMessage,
  151. $uploadNoTmpDirErrorMessage,
  152. $uploadCantWriteErrorMessage,
  153. $uploadExtensionErrorMessage,
  154. $uploadErrorMessage,
  155. $groups,
  156. $payload
  157. );
  158. $this->minWidth = $minWidth ?? $this->minWidth;
  159. $this->maxWidth = $maxWidth ?? $this->maxWidth;
  160. $this->maxHeight = $maxHeight ?? $this->maxHeight;
  161. $this->minHeight = $minHeight ?? $this->minHeight;
  162. $this->maxRatio = $maxRatio ?? $this->maxRatio;
  163. $this->minRatio = $minRatio ?? $this->minRatio;
  164. $this->minPixels = $minPixels ?? $this->minPixels;
  165. $this->maxPixels = $maxPixels ?? $this->maxPixels;
  166. $this->allowSquare = $allowSquare ?? $this->allowSquare;
  167. $this->allowLandscape = $allowLandscape ?? $this->allowLandscape;
  168. $this->allowPortrait = $allowPortrait ?? $this->allowPortrait;
  169. $this->detectCorrupted = $detectCorrupted ?? $this->detectCorrupted;
  170. $this->sizeNotDetectedMessage = $sizeNotDetectedMessage ?? $this->sizeNotDetectedMessage;
  171. $this->maxWidthMessage = $maxWidthMessage ?? $this->maxWidthMessage;
  172. $this->minWidthMessage = $minWidthMessage ?? $this->minWidthMessage;
  173. $this->maxHeightMessage = $maxHeightMessage ?? $this->maxHeightMessage;
  174. $this->minHeightMessage = $minHeightMessage ?? $this->minHeightMessage;
  175. $this->minPixelsMessage = $minPixelsMessage ?? $this->minPixelsMessage;
  176. $this->maxPixelsMessage = $maxPixelsMessage ?? $this->maxPixelsMessage;
  177. $this->maxRatioMessage = $maxRatioMessage ?? $this->maxRatioMessage;
  178. $this->minRatioMessage = $minRatioMessage ?? $this->minRatioMessage;
  179. $this->allowSquareMessage = $allowSquareMessage ?? $this->allowSquareMessage;
  180. $this->allowLandscapeMessage = $allowLandscapeMessage ?? $this->allowLandscapeMessage;
  181. $this->allowPortraitMessage = $allowPortraitMessage ?? $this->allowPortraitMessage;
  182. $this->corruptedMessage = $corruptedMessage ?? $this->corruptedMessage;
  183. }
  184. }