Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Save Base64 .png file to public folder from controller

Tags:

I send a png image file to controller in base64 via Ajax. I've already test and sure that controller has received id but still can't save it to public folder.

Here is my controller

public function postTest() {         $data = Input::all();          //get the base-64 from data         $base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);          //decode base64 string         $image = base64_decode($base64_str);         $png_url = "product-".time().".png";         $path = public_path('img/designs/' . $png_url);          Image::make($image->getRealPath())->save($path);         // I've tried using          // $result = file_put_contents($path, $image);          // too but still not working          $response = array(             'status' => 'success',         );         return Response::json( $response  ); } 
like image 625
Expl0de Avatar asked Nov 06 '14 17:11

Expl0de


People also ask

How do I save base64 images in laravel?

For uploading base64 image in laravel we need to convert and exploade image and then save base64 encoded image to file using laravel php then we can save it png, jpg.

How do I save a base64 file?

How to convert Base64 to file. Paste your string in the “Base64” field. Press the “Decode Base64 to File” button. Click on the filename link to download the file.

Can a PNG be base64?

The PNG images are binary files but the base64 strings are textual data. It's often convenient to encode PNG to base64 as it allows you to save images in text files. This utility also allows you to specify the length of base64 lines.

Where do I put laravel files?

To store files in folder storage/app , you must use Storage class as: Storage::disk('local')->put('file. txt', 'Contents'); then it would store a file in storage/app/file.


1 Answers

Intervention Image gets binary data using file_get_content function: Reference : Image::make

Your controller should be look like this:

public function postTest() {     $data = Input::all();     $png_url = "product-".time().".png";     $path = public_path().'img/designs/' . $png_url;      Image::make(file_get_contents($data->base64_image))->save($path);          $response = array(         'status' => 'success',     );     return Response::json( $response  );  } 
like image 115
Mohammad Tasneem Faizyab Avatar answered Oct 12 '22 03:10

Mohammad Tasneem Faizyab