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 ); }
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 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.
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.
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.
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 ); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With