8000 feature #23431 [Validator] Add min/max amount of pixels to Image cons… · symfony/symfony@7796331 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7796331

Browse files
committed
feature #23431 [Validator] Add min/max amount of pixels to Image constraint (akeeman)
This PR was merged into the 3.4 branch. Discussion ---------- [Validator] Add min/max amount of pixels to Image constraint | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | symfony/symfony-docs#7756 Useful for asserting image sizes/areas in pixels, for instance to estimate processing work load. **This PR continues #22288**. I cleaned up my associated branch a while ago and found no way of restoring or re-linking. @fabpot: "Could you rename to [TOO_FEW_PIXEL_ERROR](https://github.com/akeeman/symfony/blob/9ab5263d712881f8aa24920631685d14acd3a19b/src/Symfony/Component/Validator/Constraints/Image.php#L28)? Same for the other constant?" This is done. @fabpot "Can you change the base to 3.4 instead of master and rebase on current 3.4? Thanks." This is done too. Commits ------- 9ab5263 add minimum and maximum amount of pixels to Image validator
2 parents 82036a3 + 9ab5263 commit 7796331

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
deprecated and will throw an exception in Symfony 4.0
99
* setting the `checkDNS` option of the `Url` constraint to `true` is deprecated in favor of
1010
the `Url::CHECK_DNS_TYPE_*` constants values and will throw an exception in Symfony 4.0
11+
* added min/max amount of pixels check to `Image` constraint via `minPixels` and `maxPixels`
1112

1213
3.3.0
1314
-----

src/Symfony/Component/Validator/Constraints/Image.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Image extends File
2525
const TOO_NARROW_ERROR = '9afbd561-4f90-4a27-be62-1780fc43604a';
2626
const TOO_HIGH_ERROR = '7efae81c-4877-47ba-aa65-d01ccb0d4645';
2727
const TOO_LOW_ERROR = 'aef0cb6a-c07f-4894-bc08-1781420d7b4c';
28+
const TOO_FEW_PIXEL_ERROR = '1b06b97d-ae48-474e-978f-038a74854c43';
29+
const TOO_MANY_PIXEL_ERROR = 'ee0804e8-44db-4eac-9775-be91aaf72ce1';
2830
const RATIO_TOO_BIG_ERROR = '70cafca6-168f-41c9-8c8c-4e47a52be643';
2931
const RATIO_TOO_SMALL_ERROR = '59b8c6ef-bcf2-4ceb-afff-4642ed92f12e';
3032
const SQUARE_NOT_ALLOWED_ERROR = '5d41425b-facb-47f7-a55a-de9fbe45cb46';
@@ -45,6 +47,8 @@ class Image extends File
4547
self::TOO_NARROW_ERROR => 'TOO_NARROW_ERROR',
4648
self::TOO_HIGH_ERROR => 'TOO_HIGH_ERROR',
4749
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
50+
self::TOO_FEW_PIXEL_ERROR => 'TOO_FEW_PIXEL_ERROR',
51+
self::TOO_MANY_PIXEL_ERROR => 'TOO_MANY_PIXEL_ERROR',
4852
self::RATIO_TOO_BIG_ERROR => 'RATIO_TOO_BIG_ERROR',
4953
self::RATIO_TOO_SMALL_ERROR => 'RATIO_TOO_SMALL_ERROR',
5054
self::SQUARE_NOT_ALLOWED_ERROR => 'SQUARE_NOT_ALLOWED_ERROR',
@@ -60,6 +64,8 @@ class Image extends File
6064
public $minHeight;
6165
public $maxRatio;
6266
public $minRatio;
67+
public $minPixels;
68+
public $maxPixels;
6369
public $allowSquare = true;
6470
public $allowLandscape = true;
6571
public $allowPortrait = true;
@@ -72,6 +78,8 @@ class Image extends File
7278
public $minWidthMessage = 'The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px.';
7379
public $maxHeightMessage = 'The image height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px.';
7480
public $minHeightMessage = 'The image height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px.';
81+
public $minPixelsMessage = 'The image has too few pixels ({{ pixels }} pixels). Minimum amount expected is {{ min_pixels }} pixels.';
82+
public $maxPixelsMessage = 'The image has too many pixels ({{ pixels }} pixels). Maximum amount expected is {{ max_pixels }} pixels.';
7583
public $maxRatioMessage = 'The image ratio is too big ({{ ratio }}). Allowed maximum ratio is {{ max_ratio }}.';
7684
public $minRatioMessage = 'The image ratio is too small ({{ ratio }}). Minimum ratio expected is {{ min_ratio }}.';
7785
public $allowSquareMessage = 'The image is square ({{ width }}x{{ height }}px). Square images are not allowed.';

src/Symfony/Component/Validator/Constraints/ImageValidator.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function validate($value, Constraint $constraint)
4646

4747
if (null === $constraint->minWidth && null === $constraint->maxWidth
4848
&& null === $constraint->minHeight && null === $constraint->maxHeight
49+
&& null === $constraint->minPixels && null === $constraint->maxPixels
4950
&& null === $constraint->minRatio && null === $constraint->maxRatio
5051
&& $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait
5152
&& !$constraint->detectCorrupted) {
@@ -127,6 +128,40 @@ public function validate($value, Constraint $constraint)
127128
}
128129
}
129130

131+
$pixels = $width * $height;
132+
133+
if (null !== $constraint->minPixels) {
134+
if (!ctype_digit((string) $constraint->minPixels)) {
135+
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum amount of pixels', $constraint->minPixels));
136+
}
137+
138+
if ($pixels < $constraint->minPixels) {
139+
$this->context->buildViolation($constraint->minPixelsMessage)
140+
->setParameter('{{ pixels }}', $pixels)
141+
->setParameter('{{ min_pixels }}', $constraint->minPixels)
142+
->setParameter('{{ height }}', $height)
143+
->setParameter('{{ width }}', $width)
144+
->setCode(Image::TOO_FEW_PIXEL_ERROR)
145+
->addViolation();
146+
}
147+
}
148+
149+
if (null !== $constraint->maxPixels) {
150+
if (!ctype_digit((string) $constraint->maxPixels)) {
151+
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum amount of pixels', $constraint->maxPixels));
152+
}
153+
154+
if ($pixels > $constraint->maxPixels) {
155+
$this->context->buildViolation($constraint->maxPixelsMessage)
156+
->setParameter('{{ pixels }}', $pixels)
157+
->setParameter('{{ max_pixels }}', $constraint->maxPixels)
158+
->setParameter('{{ height }}', $height)
159+
->setParameter('{{ width }}', $width)
160+
->setCode(Image::TOO_MANY_PIXEL_ERROR)
161+
->addViolation();
162+
}
163+
}
164+
130165
$ratio = round($width / $height, 2);
131166

132167
if (null !== $constraint->minRatio) {

src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,42 @@ public function testHeightTooBig()
164164
->assertRaised();
165165
}
166166

167+
public function testPixelsTooFew()
168+
{
169+
$constraint = new Image(array(
170+
'minPixels' => 5,
171+
'minPixelsMessage' => 'myMessage',
172+
));
173+
174+
$this->validator->validate($this->image, $constraint);
175+
176+
$this->buildViolation('myMessage')
177+
->setParameter('{{ pixels }}', '4')
178+
->setParameter('{{ min_pixels }}', '5')
179+
->setParameter('{{ height }}', '2')
180+
->setParameter('{{ width }}', '2')
181+
->setCode(Image::TOO_FEW_PIXEL_ERROR)
182+
->assertRaised();
183+
}
184+
185+
public function testPixelsTooMany()
186+
{
187+
$constraint = new Image(array(
188+
'maxPixels' => 3,
189+
'maxPixelsMessage' => 'myMessage',
190+
));
191+
192+
$this->validator->validate($this->image, $constraint);
193+
194+
$this->buildViolation('myMessage')
195+
->setParameter('{{ pixels }}', '4')
196+
->setParameter('{{ max_pixels }}', '3')
197+
->setParameter('{{ height }}', '2')
198+
->setParameter('{{ width }}', '2')
199+
->setCode(Image::TOO_MANY_PIXEL_ERROR)
200+
->assertRaised();
201+
}
202+
167203
/**
168204
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
169205
*/
@@ -212,6 +248,30 @@ public function testInvalidMaxHeight()
212248
$this->validator->validate($this->image, $constraint);
213249
}
214250

251+
/**
252+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
253+
*/
254+
public function testInvalidMinPixels()
255+
{
256+
$constraint = new Image(array(
257+
'minPixels' => '1abc',
258+
));
259+
260+
$this->validator->validate($this->image, $constraint);
261+
}
262+
263+
/**
264+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
265+
*/
266+
public function testInvalidMaxPixels()
267+
{
268+
$constraint = new Image(array(
269+
'maxPixels' => '1abc',
270+
));
271+
272+
$this->validator->validate($this->image, $constraint);
273+
}
274+
215275
public function testRatioTooSmall()
216276
{
217277
$constraint = new Image(array(

0 commit comments

Comments
 (0)
0