Laravel中的Base64图像解码

时间:2017-09-01 10:06:10

标签: php laravel base64

我有图片上传功能:

public function upload(UploadImageRequest $request)
{
    $storagePath=Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();

    $image = $request->file('image')->storeAs('public/images', time().'_'.$request->file('image')->getClientOriginalName());

    $url = Storage::url($image);
    $image = Image::make([]);

    $image->path = $storagePath.$url;
    $image->url = "$url";
    $image->saveOrFail();

    return Response::json($image, ResponseHttp::HTTP_CREATED);
}

它工作正常,但如果前端在base64中向我发送图片,我想添加功能。怎么解码?

1 个答案:

答案 0 :(得分:1)

要解码base64图像,请使用此 -

$decode = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image));

希望这会对你有所帮助。