Skip to content

Commit

Permalink
[ImageValidator] Added dedicated ImageValidator class with min width,…
Browse files Browse the repository at this point in the history
… max width, min height and max height validations
  • Loading branch information
benjamindulau authored and stof committed Sep 4, 2011
1 parent dccd2d5 commit a5a2dfa
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/Symfony/Component/Validator/Constraints/Image.php
Expand Up @@ -19,13 +19,15 @@
class Image extends File
{
public $mimeTypes = 'image/*';
public $mimeTypesMessage = 'This file is not a valid image';
public $minWidth = null;
public $maxWidth = null;
public $maxHeight = null;
public $minHeight = null;

/**
* @inheritDoc
*/
public function validatedBy()
{
return __NAMESPACE__.'\FileValidator';
}
public $mimeTypesMessage = 'This file is not a valid image';
public $notDetectedMessage = 'The size of image could not be detected';
public $maxWidthMessage = 'The image width is too big ({{ width }}px). Allowed maximum width is {{ maxWidth }}px';
public $minWidthMessage = 'The image width is too small ({{ width }}px). Minimum width expected is {{ minWidth }}px';
public $maxHeightMessage = 'The image height is too big ({{ height }}px). Allowed maximum width is {{ maxHeight }}px';
public $minHeightMessage = 'The image width is too small ({{ height }}px). Minimum height expected is {{ minHeight }}px';
}
108 changes: 108 additions & 0 deletions src/Symfony/Component/Validator/Constraints/ImageValidator.php
@@ -0,0 +1,108 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
* Validates whether a value is a valid image file and is valid
* against minWidth, maxWidth, minHeight and maxHeight constraints
*
* @author Benjamin Dulau <benjamin.dulau@gmail.com>
*/
class ImageValidator extends FileValidator
{
public function isValid($value, Constraint $constraint)
{
$isValid = parent::isValid($value, $constraint);
if (!$isValid) {
return false;
}

if (null === $value || '' === $value) {
return true;
}

$size = @getimagesize($value);
if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) {
$this->setMessage($constraint->notDetectedMessage);

return false;
}

$width = $size[0];
$height = $size[1];

if ($constraint->minWidth) {
if (!ctype_digit((string)$constraint->minWidth)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
}

if ($width < $constraint->minWidth) {
$this->setMessage($constraint->minWidthMessage, array(
'{{ width }}' => $width,
'{{ minWidth }}' => $constraint->minWidth
));

return false;
}
}

if ($constraint->maxWidth) {
if (!ctype_digit((string)$constraint->maxWidth)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
}

if ($width > $constraint->maxWidth) {
$this->setMessage($constraint->maxWidthMessage, array(
'{{ width }}' => $width,
'{{ maxWidth }}' => $constraint->maxWidth
));

return false;
}
}

if ($constraint->minHeight) {
if (!ctype_digit((string)$constraint->minHeight)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
}

if ($height < $constraint->minHeight) {
$this->setMessage($constraint->minHeightMessage, array(
'{{ height }}' => $height,
'{{ minHeight }}' => $constraint->minHeight
));

return false;
}
}

if ($constraint->maxHeight) {
if (!ctype_digit((string)$constraint->maxHeight)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
}

if ($height > $constraint->maxHeight) {
$this->setMessage($constraint->maxHeightMessage, array(
'{{ height }}' => $height,
'{{ maxHeight }}' => $constraint->maxHeight
));

return false;
}
}

return true;
}
}
@@ -0,0 +1,166 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\ImageValidator;

class ImageValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $validator;
protected $path;
protected $image;

protected function setUp()
{
$this->validator = new ImageValidator();
$this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'ImageValidatorTest';
$this->image = imagecreatetruecolor(2, 2);
imagejpeg($this->image, $this->path);
}

protected function tearDown()
{
imagedestroy($this->image);
}

public function testNullIsValid()
{
$this->assertTrue($this->validator->isValid(null, new Image()));
}

public function testEmptyStringIsValid()
{
$this->assertTrue($this->validator->isValid('', new Image()));
}

public function testValidImage()
{
$this->assertTrue($this->validator->isValid($this->path, new Image()));
}

public function testValidSize()
{
$constraint = new Image(array(
'minWidth' => 1,
'maxWidth' => 2,
'minHeight' => 1,
'maxHeight' => 2,
));

$this->assertTrue($this->validator->isValid($this->path, $constraint));
}

public function testWidthTooSmall()
{
$constraint = new Image(array(
'minWidth' => 3,
'minWidthMessage' => 'myMessage',
));

$this->assertFalse($this->validator->isValid($this->path, $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ width }}' => '2',
'{{ minWidth }}' => '3',
));
}

public function testWidthTooBig()
{
$constraint = new Image(array(
'maxWidth' => 1,
'maxWidthMessage' => 'myMessage',
));

$this->assertFalse($this->validator->isValid($this->path, $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ width }}' => '2',
'{{ maxWidth }}' => '1',
));
}

public function testHeightTooSmall()
{
$constraint = new Image(array(
'minHeight' => 3,
'minHeightMessage' => 'myMessage',
));

$this->assertFalse($this->validator->isValid($this->path, $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ height }}' => '2',
'{{ minHeight }}' => '3',
));
}

public function testHeightTooBig()
{
$constraint = new Image(array(
'maxHeight' => 1,
'maxHeightMessage' => 'myMessage',
));

$this->assertFalse($this->validator->isValid($this->path, $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ height }}' => '2',
'{{ maxHeight }}' => '1',
));
}

public function testInvalidMinWidth()
{
$constraint = new Image(array(
'minWidth' => '1abc',
));

$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');

$this->validator->isValid($this->path, $constraint);
}

public function testInvalidMaxWidth()
{
$constraint = new Image(array(
'maxWidth' => '1abc',
));

$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');

$this->validator->isValid($this->path, $constraint);
}

public function testInvalidMinHeight()
{
$constraint = new Image(array(
'minHeight' => '1abc',
));

$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');

$this->validator->isValid($this->path, $constraint);
}

public function testInvalidMaxHeight()
{
$constraint = new Image(array(
'maxHeight' => '1abc',
));

$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');

$this->validator->isValid($this->path, $constraint);
}
}

0 comments on commit a5a2dfa

Please sign in to comment.