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

后端 未结 8 1735
灰色年华
灰色年华 2021-02-05 07:46

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.

相关标签:
8条回答
  • 2021-02-05 08:15

    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  );
     }
    
    0 讨论(0)
  • 2021-02-05 08:15
    $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.';
    
    0 讨论(0)
提交回复
热议问题