无法显示图像,因为其中包含laravel错误

问题描述 投票:1回答:1

我正在一个laravel项目中,尝试在其中使用url显示图像但出现错误。我已经尝试了很多,并搜索了所有内容,但我不明白为什么会收到此错误。我正在使用以下功能

`public function displayImage($filename){

    $path = storage_path("app/taskImage/".$filename);

    if (!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);

    $type = File::mimeType($path);

    $response = \Response::make($file, 200);

    $response->header("Content-Type", $type);

    return $response;
}`

route

Route::get('taskImg/{filename?}', [ 'uses' => 'FormController@displayImage', ]);

我正在练习的URL就像

http://localhost/project_name/public/taskImg/test.jpg

当我打印了一些东西时,我得到了,但是没有得到图像。它显示空白屏幕,并显示错误消息The image cannot be displayed because it contains errors

laravel laravel-5.7
1个回答
0
投票

您可以这样操作

$storagePath = storage_path("app/taskImage/".$filename);

return Image::make($storagePath)->response();
© www.soinside.com 2019 - 2024. All rights reserved.