Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

roemer's avatar

Image validation fails with SVG

Hi everyone,

A client of mine complained today that one of his customers can't upload their SVG logo into an App. I found this odd since I validate for image and the documentation clearly states:

The file under validation must be an image (jpeg, png, bmp, gif, or svg)

Still, the request was denied. The validation message for an invalid image shows up as the response for the request (REST API). In short, here's my code:

$validator = Validator::make($request->all(), [
    'logo' => 'required|image|max:20000'
]);

I've also tried it like this, but that doesn't work either, same message.

$validator = Validator::make($request->all(), [
    'logo' => 'required|mimes:jpg,jpeg,png,gif,svg|max:20000'
]);

Can anyone spot what's going wrong? Thanks!

0 likes
6 replies
Romain's avatar

Hey,

maybe try "size" instead of "max" even though it should be doing the same thing.

And also, check with a service what the image really is. Sometimes people try to use images that have been changed many times. From Bitmap, to jpeg, transformed to git, back into a png.

So maybe the image is to blame too. Also a .jpeg file can actually be a png file. The extension is not enough to determine what type a file really is.

Also, maybe to narrow it down, create specific error messages for each of the condition, so that you can be sure if it fails on the "required", "mimes" or "size".

roemer's avatar

@Ramain Thanks for the suggestions! I checked online and the sample image I was trying had mime-type text/plain, I suppose that that's why it wasn't working. Did get recognized as a SVG image by OS X though..

Anyway, I seem to have a new problem. I tried with another file which is definitely SVG (https://commons.wikimedia.org/wiki/File:Example.svg checked with http://www.checkfiletype.com/upload-and-check) but now I'm getting this exception:

NotReadableException in Decoder.php line 20:
Unable to read image from file (/tmp/phpoRb7Og).
in Decoder.php line 20
at Decoder->initFromPath('/tmp/phpoRb7Og') in AbstractDecoder.php line 314
at AbstractDecoder->init('/tmp/phpoRb7Og') in AbstractDriver.php line 64
at AbstractDriver->init('/tmp/phpoRb7Og') in ImageManager.php line 50
at ImageManager->make('/tmp/phpoRb7Og') in RealestateAgencyRepository.php line 361

Seems like Decoder doesn't handle the file for some reason? It's not a permissions thing, every other type of image works.

P.S. Also tried your size, but that seems to validate for minimum (or exact, not sure) file size, so anything lower than 20MB get's declined.

Cronix's avatar

Pretty sure the problem is because svg's don't really have dimensions like real images, because they're scalable vectors so they can be any size. They're xml files.

What you could try to do is detect the mime type of the uploaded image, and if it's NOT svg, add the max rule to your validation.

something like

$logoRules = 'required|mimes:jpg,jpeg,png,gif,svg';

//do the check if it's svg, then

if ( ! $svg) {
    $logoRules .= '|max:20000';
}

$validator = Validator::make($request->all(), [
    'logo' => $logoRules
]);
seanthompson's avatar

We have encountered this very issue and have discovered, in 5.5, all you need to do is add:

'required|mimes:svg,html';

2 likes
hchiter's avatar

@seanthompson had it right, just add mimes:svg,html make sure that you have a comma between svg and html and this should fix the problem

Please or to participate in this conversation.