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

PacketReaver's avatar

Image validation from string

Hello, I have an image in a string variable and I would like to use the built-in Laravel Validation to... validate it.

I've tried something like this:

$imageString = file_get_contents('https://example.com/img.jpg');
Validator::make(['image' => $imageString], ['image' => 'required|image|max:5120']);

but it's not working. I guess the question is what data exactly Laravel expects to properly validate the image. I know that normally the image comes from Request object, but is it possible to use some trick to verify the image downloaded inside the code? (like with file_get_contents)

0 likes
5 replies
LaryAI's avatar
Level 58

To validate an image from a string variable using Laravel's built-in validation, you can use the data:// stream wrapper to create a data URL from the image string. Then, you can pass this data URL to the validation rules.

Here's an example:

$imageString = file_get_contents('https://example.com/img.jpg');
$dataUrl = 'data:image/jpeg;base64,' . base64_encode($imageString);

Validator::make(['image' => $dataUrl], [
    'image' => 'required|image|max:5120',
]);

In this example, we're assuming that the image is a JPEG. If it's a different format, you'll need to adjust the image/jpeg part of the data URL accordingly.

Note that this approach will base64-encode the image, which will increase its size by about 33%. If you're dealing with large images, this could be a problem. In that case, you might want to consider using a different approach, such as saving the image to a temporary file and validating it using Laravel's File validation rules.

Snapey's avatar

it can't be in a string unless you have base64 encoded it

Laravel has no built in way to determine an image from a string

PacketReaver's avatar

@Snapey I tried the method given by Lary and unfortunately it doesn't work either - Laravel 9 didn't recognize that it was an image :(

martleby's avatar

If anybody stumbles across this, here's what I did without using external packages. Use case is that an image is manipulated in the frontend and sent via a hidden field that has the base64 encoded image data in it.

		//First check that the request has the input - in this case it's a hidden field in blade with the base64 encoded string
		$rules = ['photo' => 'required'];
        $validated = $request->validate($rules);


		//Next we need to save the data to a temp file
        $tempDisk = Storage::build([
            'driver' => 'local',
            'root' => sys_get_temp_dir()
        ]);

        $tmpFileName = uniqid('Tmp_Photos', false) . '.jpg';

       //Decode the image
        $photo = base64_decode(explode(',', $validated['photo'])[1]);

        $tempDisk->put($tmpFileName, $photo);

        //Next create a new File instance pointing to our temp file
        $file = new File($tempDisk->path($tmpFileName));

       //And then we can work with Laravel's validation
        Validator::make(['photo' => $file], [
            'photo' => 'required|file|image|mimes:jpeg,jpg|max:2048',
        ])->validate();

        //do stuff with the validated image...

		//Finally clear up after ourselves		
        $tempDisk->delete($tmpFileName);

This is a bit naive, and only works for jpgs. You should probably do some exception handling to make sure the file gets cleared up.

Please or to participate in this conversation.