-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Adding image size validation #9465
Conversation
@@ -1082,6 +1082,76 @@ public static function uploadedFile($file, array $options = []) | |||
} | |||
|
|||
/** | |||
* Validates the size of an uploaded image. | |||
* | |||
* @param array $value |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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
OT: I wish I could auto ignore stickler-ci-mail-spam unless it is my own PR. |
|
This is a cool feature. thanks. I think anyone working with images will have GD installed. |
It doesn't require GD, this is all built-in. It however doesn't guarantee that what you're testing is really an image. |
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
|
throw new InvalidArgumentException('Invalid image size validation parameters! Missing `width` and / or `height`!'); | ||
} | ||
|
||
list($width, $height) = getimagesize($file['tmp_name']); |
There was a problem hiding this comment.
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()
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Current coverage is 94.92% (diff: 92.59%)@@ 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
|
*/ | ||
public static function imageSize($file, $options) | ||
{ | ||
if (!isset($options['height']) && !isset($options['width'])) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@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. |
Then its no point doing more checks. |
There was a problem hiding this 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`!'); |
There was a problem hiding this comment.
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']); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Why when already built into php
|
@jimgwhit This isn't a file size validation, but an image size (i.e width and height) validation. |
@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. |
This looks good to me |
This works without any external extension or lib, see
Please see http://php.net/manual/en/function.getimagesize.php