尝试显示以 base64 编码的图像

Trying to display image encoded in base64

我正在尝试在我的浏览器上显示我的图像(以 base64 编码)。

这是我试过的:

        {{$projects}}
        <hr>
        @foreach ($projects as $project)
            @if ($project['name'] != null)
                <p>Project {{ $project['name'] }}</p>
                <img src="data:image/{{$project['image_type']}};base64,{{$project['image']}}" alt="Project picture" >
            @endif
        @endforeach

在浏览器上看起来像这样 link:http://i.imgur.com/U1YUcSo.png

不幸的是没有成功。

显示图像之前。我使用以下代码将图像上传到我的数据库:

public function store(Request $request)
{   
    if($request['file'] != null) {
        $file = $request['file'];
        $fileName = $file->getClientOriginalName();
        $imageType = pathinfo($fileName, PATHINFO_EXTENSION);

    } else {
        $fileName = null;
        $imageType = null;
    }
    Project::create([
        'name' => $request['name'],
        'content' => $request['content'],
        'image' => base64_encode($fileName),
        'image_type' => $imageType,
    ]);

    return redirect('/projects');
}

有人可以告诉我我做错了什么吗?

您似乎在对数据库中的文件名而不是文件内容进行编码。

了解 http://php.net/manual/pt_BR/function.file-get-contents.php

(...)
'image' => base64_encode(file_get_contents($fileName)),

Niloct 是对的。

而不是

base64_encode($fileName);

你必须使用

base64_encode(file_get_contents($fileName));