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

怎甘沉沦 提交于 2019-12-03 15:53:47

问题


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  );
}

回答1:


Intervention Image gets binary data using file_get_content function: Reference : http://image.intervention.io/api/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  );
 }



回答2:


This is an easy mistake.

You are using public_path incorrectly. It should be:

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

Also, I would avoid your method of sending the image. Look at a proper upload in a form and use Laravel's Input::file method.




回答3:


$file = base64_decode($request['image']);
        $folderName = 'public/uploads/';
        $safeName = str_random(10).'.'.'png';
        $destinationPath = public_path() . $folderName;
        $success = file_put_contents(public_path().'/uploads/'.$safeName, $file);
        print $success;



回答4:


$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.';



回答5:


My solution is:

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  );
}



回答6:


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

$data = Input::all();

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

$data->base64_image

So, it's not working. You should try using:

$image = $data['base64_image'];

Since it's (base64_image) accessible from $_POST then Input::file('base64_image') won't work because Input::file('base64_image') checks the $_FILES array and it's not there in your case.




回答7:


what am i doing is using basic 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;
        }



回答8:


I'v done it!!

I replaced $data->base64_image to $_POST['base64_image'] and then use $result = file_put_contents($path, $image); instead of Image::make($image->getRealPath())->save($path);

But this doesn't look like a laravel ways. I you have another way that look more elegant please tell me!



来源:https://stackoverflow.com/questions/26785940/laravel-save-base64-png-file-to-public-folder-from-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!