How to Base64 Image Upload in Laravel?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this example,I will show you how to easy convert base64 image upload in laravel application. We can do easy and simply convert to base64 image upload in laravel application. Now here i will write very simple code for save base64 encoded image to file by using php and also we can save it png, jpg as we want.

While we are working with API for app, then we will notice that they will send the format of images in Base64 encoded. That’s why in that case, we need to move the Base64 encoded images to server as a image file.

So here now in this example i write function createImage() from base64 string. So from here you can simply follow bellow example:

example :
<?php

    createImage($_POST['image']);

    public function createImage($img)
    {

        $folderPath = "images/";

        $image_parts = explode(";base64,", $img);
        $image_type_aux = explode("image/", $image_parts[0]);
        $image_type = $image_type_aux[1];
        $image_base64 = base64_decode($image_parts[1]);
        $file = $folderPath . uniqid() . '. '.$image_type;

        file_put_contents($file, $image_base64);

    }

?>

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →