brandonsavage / Upload

File uploads with validation and storage strategies
MIT License
1.67k stars 314 forks source link

Image width / height validation #56

Closed Langmans closed 9 years ago

Langmans commented 9 years ago

Title says it all :)

Proposed class:

<?php

namespace Upload\Validation;

use Upload\Exception;
use Upload\FileInfoInterface;
use Upload\ValidationInterface;

class ImageSize implements ValidationInterface
{
    function __construct($width, $height)
    {
        $this->width = $width;
        $this->height = $height;
    }

    public function validate(FileInfoInterface $info)
    {
        $dimensions = $info->getDimensions();
        $filename = $info->getNameWithExtension();
        if (!$dimensions) {
            throw new Exception(sprintf('%s: Could not detect image size.', $filename));
        }
        if ($dimensions['width'] != $this->width) {
            throw new Exception(
                sprintf(
                    '%s: Image width(%dpx) does not match required width(%dpx)',
                    $filename,
                    $dimensions['width'],
                    $this->width
                )
            );
        }
        if ($dimensions['height'] != $this->height) {
            throw new Exception(
                sprintf(
                    '%s: Image height(%dpx) does not match required height(%dpx)',
                    $filename,
                    $dimensions['height'],
                    $this->height
                )
            );
        }
    }
}
valousal commented 9 years ago

Hello ! How to use this class in my controller ?

buggedcom commented 9 years ago

@rubenvincenten you should submit this as a pull request @codeguy might then pull it in.