question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Image file validation - max size issue

See original GitHub issue

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'
        ];

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
demogarcommented, Jul 21, 2018

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.

2reactions
MarcosBLcommented, Mar 1, 2017

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.

size:value

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

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:

$rules = array(
   'photo' => 'photo_rules'
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

max file size validation for file/image fields - ProcessWire
In my imagination the file/image field get a new option: max file size. The validation (simple JS check if the file selected for...
Read more >
Laravel file size validation returns error regardless of file size
size:value => The field under validation must have a size matching the given value. Use max instead of size
Read more >
File size validator should only respect the explicitly configured ...
Problem /Motivation Drupal\file\Plugin\Field\FieldType\FileItem::getUploadValidators() always sets the maximum file size for uploading based ...
Read more >
Validation of file size while uploading using JavaScript / jQuery
In this article, we will learn how to implement file size validation by checking file size before uploading using Javascript and jQuery.
Read more >
Content validation | Uploadcare Docs
Limit File Size · Restrict File Type · Image Dimensions · Image Orientation. File Size. The example below shows how to add validation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found