Laravel:将Base64 .png文件从控制器保存到公用文件夹 [英] Laravel: Save Base64 .png file to public folder from controller

查看:342
本文介绍了Laravel:将Base64 .png文件从控制器保存到公用文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Ajax将png图像文件发送到base64中的控制器.我已经测试过并确保控制器已收到ID,但仍无法将其保存到公用文件夹中.

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.

这是我的控制人

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

推荐答案

干预图像使用file_get_content函数获取二进制数据: 参考: http://image.intervention.io/api/make

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

这篇关于Laravel:将Base64 .png文件从控制器保存到公用文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆