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

eejaz's avatar
Level 2

create image from base64 string laravel

Hi I'm writing an API To Upload Image form Android Phone trying to convert the base64 string to image and store in public path storage. now the problem is that: the file is stored but can't open error: image not loaded, try to open it externally to fix formate problem

this is my code for the controller

        //get the base-64 from data
        $base64_str = substr($arc->document3, strpos($arc->document3, ",")+1);

        //decode base64 string
        $image = base64_decode($base64_str);

        $safeName = str_random(10).'.'.'png';
        Storage::disk('public')->put('eejaz/'.$safeName, $image);
0 likes
15 replies
eejaz's avatar
Level 2

@ekhlas Thanks, but this concept is for the normal file, $request->file(), in this case,I use base64 string.

eejaz's avatar
eejaz
OP
Best Answer
Level 2

finally, I get the solution.

        $image = $request->image;  // your base64 encoded
        $image = str_replace('data:image/png;base64,', '', $image);
        $image = str_replace(' ', '+', $image);
        $imageName = str_random(10).'.'.'png';
        \File::put(storage_path(). '/' . $imageName, base64_decode($image));

This the Code for covert any Base64 String to Image and store theme in a local path with file name.

23 likes
Amilkar's avatar

It works for me:

if .jpg

    $image = str_replace('data:image/jpeg;base64,', '', $image);
    $image = str_replace(' ', '+', $image);
    $imageName = str_random(10).'.'.'jpg';

or .png

    $image = str_replace('data:image/png;base64,', '', $image);
    $image = str_replace(' ', '+', $image);
    $imageName = str_random(10).'.'.'png';
1 like
afridi's avatar

How do you know that the Encdoed image is in that specific format? Or you are just imagining it.

SylarRuby's avatar

If you do not know the file extension:

 $img = preg_replace('/^data:image\/\w+;base64,/', '', $image);
 $type = explode(';', $image)[0];
 $type = explode('/', $type)[1]; // png or jpg etc
hangingbrain's avatar

I tried this, but i failed to get the right file extension. But thanks to your code I was able to recreate it and found a solution to this.

The code below gives the extension of the file.

        $ext = explode(';base64',$image);
        $ext = explode('/',$ext[0]);			
        $ext = $ext[1];						// This will hold the value of the extension.
ambalavanbasith's avatar

All types of file can be solved by using this ,

  $image_64 = $data['photo']; //your base64 encoded data

  $extension = explode('/', explode(':', substr($image_64, 0, strpos($image_64, ';')))[1])[1];   // .jpg .png .pdf

  $replace = substr($image_64, 0, strpos($image_64, ',')+1); 

// find substring fro replace here eg: data:image/png;base64,

 $image = str_replace($replace, '', $image_64); 

 $image = str_replace(' ', '+', $image); 

 $imageName = Str::random(10).'.'.$extension;

 Storage::disk('public')->put($imageName, base64_decode($image));
6 likes
JackBruce's avatar

Hello Brother, I have tried your method in laravel 6, while save in folder the image get corrupted, is there anything you can help?

JohnJoshua's avatar

Hi :) I'm using a markdown editor (toast-ui) and I try to get all the images base64 included in the text field to record them as an image but I can't find a way to loop for this. Any help would be VERY appreciated :) Thanks!

User1980's avatar

@Snapey I checked on this, unfortunately it looks like Intervention does not work on Laravel 6

Please or to participate in this conversation.