Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding image size validation #9465

Merged
merged 6 commits into from
Sep 21, 2016
Merged

Adding image size validation #9465

merged 6 commits into from
Sep 21, 2016

Conversation

burzum
Copy link
Contributor

@burzum burzum commented Sep 16, 2016

This works without any external extension or lib, see

Please see http://php.net/manual/en/function.getimagesize.php

Note: This function does not require the GD image library.

@@ -1082,6 +1082,76 @@ public static function uploadedFile($file, array $options = [])
}

/**
* Validates the size of an uploaded image.
*
* @param array $value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment

* Validates the size of an uploaded image.
*
* @param array $value
* @param array $options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment

*
* @param array $value
* @param array $options
* @return boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected "bool" but found "boolean" for function return type

/**
* Validates the image width.
*
* @param array $value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment

* Validates the image width.
*
* @param array $value
* @param string $operator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment

*
* @param array $value
* @param string $operator
* @param integer $width
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment
Expected "int" but found "integer" for parameter type

* @param array $value
* @param string $operator
* @param integer $width
* @return boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected "bool" but found "boolean" for function return type

/**
* Validates the image width.
*
* @param array $value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment

* Validates the image width.
*
* @param array $value
* @param string $operator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment

*
* @param array $value
* @param string $operator
* @param integer $height
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter comment
Expected "int" but found "integer" for parameter type

* @param array $value
* @param string $operator
* @param integer $height
* @return boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected "bool" but found "boolean" for function return type

*
* @return void
*/
public function testImageSize() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening brace should be on a new line

@inoas
Copy link
Contributor

inoas commented Sep 16, 2016

OT: I wish I could auto ignore stickler-ci-mail-spam unless it is my own PR.

@dakota
Copy link
Member

dakota commented Sep 16, 2016

getimagesize requires the GD extension to be installed?

@thinkingmedia
Copy link
Contributor

This is a cool feature. thanks. I think anyone working with images will have GD installed.

@ndm2
Copy link
Contributor

ndm2 commented Sep 16, 2016

It doesn't require GD, this is all built-in. It however doesn't guarantee that what you're testing is really an image.

@burzum
Copy link
Contributor Author

burzum commented Sep 16, 2016

It doesn't require any external lib, it's built in. That's why I've linked http://php.net/manual/en/function.getimagesize.php

Note: This function does not require the GD image library.

throw new InvalidArgumentException('Invalid image size validation parameters! Missing `width` and / or `height`!');
}

list($width, $height) = getimagesize($file['tmp_name']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good if this could handle non-uploads too, like for example mimeType().

Copy link
Contributor Author

@burzum burzum Sep 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So check if the passed value is a string and that the file exists? If yes, I can do that next week. I'm about to leave for a weekend trip.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say the other way around, check if it's an array, like in the linked method. And an is_file() check would be good too, yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also handle PSR7 UploadedFile objects? That input has a getStream() method which lets you read the underlying file data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd think that in a regular request CakePHP should never see such objects, but I guess that handling them generally wouldn't hurt in case CakePHP is being advertised with PSR7 support.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hoping that in the future people will use the uploaded files objects more than the low level array data.

@inoas
Copy link
Contributor

inoas commented Sep 16, 2016

It doesn't require GD, this is all built-in. It however doesn't guarantee that what you're testing is really an image.

Maybe an additional check for the mimeType then doesn't hurt? For all the types supported:
For example: GIF, JPEG, PNG, PSD, BMP, TIFF_II, TIFF_MM, JPC, JP2, JPX, JB2, IFF, WBMP, JPEG2000, XBM, ICO
aka: http://stackoverflow.com/a/1282373

Doesn't really help, see #9465 (comment)

@codecov-io
Copy link

codecov-io commented Sep 16, 2016

Current coverage is 94.92% (diff: 92.59%)

Merging #9465 into master will increase coverage by <.01%

@@             master      #9465   diff @@
==========================================
  Files           412        412          
  Lines         28204      28273    +69   
  Methods        3382       3387     +5   
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          26771      26838    +67   
- Misses         1433       1435     +2   
  Partials          0          0          

Powered by Codecov. Last update 996440c...4ed813f

*/
public static function imageSize($file, $options)
{
if (!isset($options['height']) && !isset($options['width'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method should allow checking for only height or width too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I misread the code, it does.

@ndm2
Copy link
Contributor

ndm2 commented Sep 16, 2016

Maybe an additional check for the mimeType then doesn't hurt?

@ionas Not sure how that would help? The mime type check usually also only relies on file header information. Without the use of an image library that checks whether the given file is actually a valid image, and doesn't only rely on the header info for the size check, there's not much to do here I guess.

@inoas
Copy link
Contributor

inoas commented Sep 16, 2016

Then its no point doing more checks.

Copy link
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good @burzum

public static function imageSize($file, $options)
{
if (!isset($options['height']) && !isset($options['width'])) {
throw new InvalidArgumentException('Invalid image size validation parameters! Missing `width` and / or `height`!');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need ! in the error messages.

throw new InvalidArgumentException('Invalid image size validation parameters! Missing `width` and / or `height`!');
}

list($width, $height) = getimagesize($file['tmp_name']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also handle PSR7 UploadedFile objects? That input has a getStream() method which lets you read the underlying file data.

'tmp_name' => $image
];

$this->assertTrue(Validation::imageWidth($upload, '>', 100));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there also >= and <= as tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@jimgwhit
Copy link

Why when already built into php

if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
} 

reference http://www.w3schools.com/php/php_file_upload.asp

@dakota
Copy link
Member

dakota commented Sep 19, 2016

@jimgwhit This isn't a file size validation, but an image size (i.e width and height) validation.

@ADmad
Copy link
Member

ADmad commented Sep 19, 2016

@jimgwhit What does file size have anything to do with validating image width and height? Please avoid posting comments which doesn't add anything constructive to the discussion.

@lorenzo
Copy link
Member

lorenzo commented Sep 20, 2016

This looks good to me

@burzum burzum self-assigned this Sep 21, 2016
@markstory markstory merged commit b2f37db into cakephp:master Sep 21, 2016
@markstory markstory deleted the feature/imagesize-validation branch September 21, 2016 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet