Laravel: save Base64.png file to a shared folder from the controller

I am sending a PNG image file to the controller in base64 via Ajax. I have already tested and am sure that the controller received the identifier, but cannot save it in a shared 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 ); } 
+6
source share
7 answers

Intervention Image gets binary data using the file_get_content function: Link: http://image.intervention.io/api/make

Your controller should 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 ); } 
+6
source

This is not an easy mistake.

You are using public_path incorrectly. It should be:

 $path = public_path() . "/img/designs/" . $png_url; 

In addition, I would avoid your method of sending an image. See the correct loading in the form and use the Laravel Input :: file method.

+4
source
 $data = Input::all(); $png_url = "perfil-".time().".jpg"; $path = public_path() . "/img/designs/" . $png_url; $img = $data['fileo']; $img = substr($img, strpos($img, ",")+1); $data = base64_decode($img); $success = file_put_contents($path, $data); print $success ? $png_url : 'Unable to save the file.'; 
+2
source

I did it! I changed $data->base64_image to $_POST['base64_image'] . And use $result = file_put_contents($path, $image); instead of Image::make($image->getRealPath())->save($path); But this does not look like laravel. I have another way to look more elegant, please tell me!

0
source

Actually, Input::all() returns an array inputs, so you have the following:

 $data = Input::all(); 

Now your $data is an array, not an object, so you are trying to access the image as an object:

 $data->base64_image 

So it does not work. You must try:

 $image = $data['base64_image']; 

Since it ( base64_image ) is accessible from $_POST , then Input::file('base64_image') will not work, because Input::file('base64_image') checks the $_FILES , and it does not exist in your case.

0
source

what i do is using the main way

 $file = base64_decode($request['profile_pic']); $folderName = '/uploads/users/'; $safeName = str_random(10).'.'.'png'; $destinationPath = public_path() . $folderName; file_put_contents(public_path().'/uploads/users/'.$safeName, $file); //save new file path into db $userObj->profile_pic = $safeName; } 
0
source

My decision:

 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); Storage::disk('local')->put('imgage.png', $image); $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix(); echo $storagePath.'imgage.png'; $response = array( 'status' => 'success', ); return Response::json( $response ); } 
0
source

Source: https://habr.com/ru/post/977843/


All Articles