When trying to limit image uploads to 10mb using the image field, I get this error message on a 4.2mb image:
The photo may not be greater than 10000 characters.
Assuming this is because its validating the base64 encoded string, not the original file size.
Here's my validation rules:
return [
'name' => 'required|max:255',
'photo' => 'max:10000'
];
According to docs, max:value
The field under validation must be less than or equal to a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.
It seems correct from the docs, but docs refer to uploaded files/images, not base64 encoded strings, I think you will need a custom validator here. Refer to https://laravel.com/docs/5.4/validation#custom-validation-rules and try something like this
php
Validator::extend('photo_rules',function($attribute, $value, $params, $validator) {
$image = base64_decode($value);
/* check mime, check file size on disk, check dimensions, whatever... */
return XXX; /* true/false if the image fits the required attributes */
});
And then your custom rule:
php
$rules = array(
'photo' => 'photo_rules'
);
Thanks @MarcosBL . I guess all uploaded files through the CRUD fields get sent as base64 strings?
Perhaps we should add a note in the docs about how to validate the file size of these.
Sure, it's detailed in base64_image https://laravel-backpack.readme.io/docs/crud-fields#section-base64_image with a link to tips and tricks in https://github.com/Laravel-Backpack/CRUD/pull/56#issue-164712261 (like how to store it on disk and bypass the database, recommended)
Or you can use image https://laravel-backpack.readme.io/docs/crud-fields#section-image on the docs you have info on how to store it on disk (so you can validate size, and delete it if greater than X)
Great response here @MarcosBL . This looks settled to me, so I'll go ahead and close the issue.
Cheers, guys!
So, if anyone else is looking for a solution to validate the file size of the image, I made the following.
Just add a new Validation to AppServiceProvider.php:
Validator::extend('base64image', function ($attribute, $value, $parameters, $validator) {
try {
$image = Image::make($value);
$size = strlen(base64_decode($value));
$size_kb = $size / 1024;
return $size_kb <= $parameters[0];
} catch (\Exception $e) {
return false;
}
});
Validator::replacer('base64image', function($message, $attribute, $rule, $parameters) {
$base64image = $parameters[0];
return str_replace(':base64image', $base64image, $message);
});
Note that I'm using Intervention for checking it is an image (I had intervention already). Then, on the validator parameter I pass the max file size (on kb), as follows:
'photo_1' => 'required_if:active,1|base64image:1000',
Cheers.
Great. Thanks for sharing your solution @demogar !
Most helpful comment
So, if anyone else is looking for a solution to validate the file size of the image, I made the following.
Just add a new Validation to
AppServiceProvider.php:Note that I'm using Intervention for checking it is an image (I had intervention already). Then, on the validator parameter I pass the max file size (on kb), as follows:
Cheers.