3
votes

I was working on a Laravel application with version 7.30.4. The project was working fine. But recently I updated the Laravel version to version 8.0.0. The application is also working normally. But the image validation rule is not working anymore.

Here are my rules

$rules = [
    'logo' => 'nullable|image|mimes:jpeg,bmp,png,jpg'
];

When I try to upload an image then it always gives the validation error

'The logo must be an image.'.

But in Laravel 7, I never faced this issue with the same file.

On my production server, the project is running on Laravel 7.30.4. I am afraid if I update the version on the production then the image validation no longer works.

The question is that, why the behavior of the image validation is not working for the Laravel 8.0 version which was working for the previous version.

Updates:-

  • I also checked that the issue with the jpg/jpeg image.
  • The PNG file is working fine but in the case jpg/jpeg image it always gives the above mention validation error.
  • when I try to upload the BMP file then it gives the error "Unsupported image type. GD driver is only able to decode JPG, PNG, GIF, or WebP files."
1
maybe the image you're uploading is too big and doesnt reach laravel as an image ? Or the image you're uploading is not really an image (even if the extention is correct)N69S
@N69S No. The same image is uploading on the production server or some other machine where the Laravel version is 7.30 working fine.Sachin Kumar
Thanx @EsTeAa I investigate the same. but what is the best way to add "jpg" other than upgrade Laravel version?Sachin Kumar

1 Answers

0
votes

I Investigate and checked, in the Laravel version(before v8.17.0) for the image validation rule, they didn't add 'jpg' for the mimes list array. That why I am not able to upload a jpg file. However "jpg" is present in the mimes list for Laravel version 7.

https://github.com/laravel/framework/blob/v8.17.0/src/Illuminate/Validation/Concerns/ValidatesAttributes.php#L1030

/**
 * Validate the MIME type of a file is an image MIME type.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @return bool
 */
public function validateImage($attribute, $value)
{
    return $this->validateMimes($attribute, $value, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp']);
}

https://github.com/laravel/framework/blob/v8.16.1/src/Illuminate/Validation/Concerns/ValidatesAttributes.php#L1030

/**
 * Validate the MIME type of a file is an image MIME type.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @return bool
 */
public function validateImage($attribute, $value)
{
    return $this->validateMimes($attribute, $value, ['jpeg', 'png', 'gif', 'bmp', 'svg', 'webp']);
}

So the solution is to upgrade the Laravel version to at least v8.17.0.

or can someone tell me the better way to update the existing list because when I try to upgrade the Laravel version there will be some compatible issues as well and also we should not change the content in the vendor folder because on the next composer update it will override or wipeout the changes?