php Laravel:将 Base64 .png 文件从控制器保存到公共文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26785940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:03:44  来源:igfitidea点击:

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

phplaravellaravel-4

提问by Expl0de

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.

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

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

回答by Mohammad Tasneem Faizyab

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

Intervention Image 使用 file_get_content 函数获取二进制数据:参考: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  );
 }

回答by nageen nayak

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

回答by Daniel Twigg

This is an easy mistake.

这是一个容易犯的错误。

You are using public_path incorrectly. It should be:

您使用的 public_path 不正确。它应该是:

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

另外,我会避免您发送图像的方法。查看表单中的正确上传并使用 Laravel 的 Input::file 方法。

回答by Carlos Mundaray

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

回答by Armando Cordova

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

回答by Learner

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

回答by The Alpha

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

实际上,Input::all()返回一个array输入,因此您有以下内容:

$data = Input::all();

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

现在您$data是一个数组而不是一个对象,因此您尝试将图像作为对象访问,例如:

$data->base64_image

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

所以,它不起作用。您应该尝试使用:

$image = $data['base64_image'];

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

由于它是(base64_image)的访问$_POSTInput::file('base64_image')不会起作用,因为Input::file('base64_image')检查的$_FILES阵列,它是不是有你的情况。

回答by Expl0de

I'v done it!!

我做到了!!

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

我替换 $data->base64_image$_POST['base64_image']然后使用 $result = file_put_contents($path, $image);代替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!

但这看起来不像 Laravel 的方式。我你有另一种看起来更优雅的方式请告诉我!