首页 > 解决方案 > 错误消息:在 null 上调用成员函数 getRealPath()

问题描述

我试图在 Heroku 上发布图像。但是图像无法生成,也没有显示在 Heroku 上。当我发布时,图像会显示在本地主机上。

一篇文章告诉我,它可以用base64显示。请帮忙。

帖子控制器

    public function store(PostRequest $request)
    {
        $image_file = "";
        $upload_image = $request->file('image_file');
        $post->image = base64_encode(file_get_contents($request->image->getRealPath())); //here is error
        if ($upload_image) {
            $path = $upload_image->store('uploads', 'public');
            if ($path) {
                Image::make($upload_image->getRealPath())->resize(150, 150)->save();
                $image_file = $path;
            }
        }

        $post  = new Post;
        $post->subject = $request->subject;
        $post->message = $request->message;
        $post->image_file = $image_file;
        $post->name = "";
        $post->user_id = Auth::id();
        $post->save();
        return redirect('/bbs')->with('poststatus', '');
    }

索引刀片

               <td>
                    @if (empty($post->image_file))
                    
                    @else
                        <img src="data:image/png;base64,<?= $post->image ?>" width="150px"> 
                    @endif
                </td> 

标签: phpheroku

解决方案


推荐阅读