如何将base64图像解码保存到laravel中的公用文件夹

Rah*_*ndi 0 php laravel laravel-4 php-7

我得到一个格式为String base64的图像,我想将该字符串解码为图像并将其保存到laravel中的公共文件夹中。

这是我的控制器:

//decode string base64 image to image 
$image = base64_decode($request->input('ttd'));
//create image name
$photo_name = time().'.png';
$destinationPath = public_path('/uploads');
//save image to folder
$image->move($destinationPath, $photo_name);
$img_url = asset('/uploads'.$photo_name);


$data = new Transaction();
$data->transaction_id = $request->input('fa_transaction_id');
$data->user_id = $request->input('userid');
$data->photo_name = $photo_name;
$data->photo_url = $img_url;
$data->save();
Run Code Online (Sandbox Code Playgroud)

当我尝试echo $ image时,我得到了解码值,而对于$ photo_name我也得到了该值,但是当函数运行时出现此错误

Call to a member function move() on string
Run Code Online (Sandbox Code Playgroud)

如何解决这个错误?

Iur*_*ari 5

//Controller

use Illuminate\Support\Facades\Storage;

//Inside method

    $image = $request->image;  // your base64 encoded
    $image = str_replace('data:image/png;base64,', '', $image);
    $image = str_replace(' ', '+', $image);
    $imageName = str_random(10) . '.png';

    Storage::disk('local')->put($imageName, base64_decode($image));
Run Code Online (Sandbox Code Playgroud)

另外,请确保您的local磁盘配置如/config/filesystems.php

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
    ]
Run Code Online (Sandbox Code Playgroud)

这样,文件将保存在 /storage/app/public目录中。

不要忘记编写php artisan storage:link该目录中的文件以使该目录中的文件可用/public,以便用户检索它们。