Validation for image size
Validation for image size

I found a function that gives you the size of an image. Width and Height. This function is getimagesize
. I want to integrate a validation using this function
->validator( function ( $file ) {
$size = getimagesize($file);
return $size[0] <= 300 && $size[1] <=450 ?
"The size must be 300 width and 450 height." :
null;
} )
It's this the correct way to do it?
This discussion has been closed.
Replies
Assuming that
validator
is attached to anUpload
instance, then yes, that looks valid and correct.Custom validation documentation for uploading files is available in the manual (you probably know that given the above code, but just linking for anyone else who reads this post!).
Allan
Thanks for the replay. I changed my code as follows:
But I got a new error:
Warning: getimagesize(WIN_20151008_13_58_09_Pro.jpg): failed to open stream: No such file or directory
It seems to me that the function needs the correct location. Where does the Upload function put the file before it uploads it?
Here is what I need to make it work. The path needs to be set in the function in order to read the width and the height.
getimagesize('path_to_image');
Try using
$file['tmp_name']
. THe PHP manual has details on this property.Allan
It worked!
I will share the code.
After validating the height/width how can those values be stored in separate database columns? I've tried using db() but without success.
You would indeed using the
db()
method with a closure function (see option 3 of option 3!). That function would return the value required.Allan