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



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

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::get('taskImg/{filename?}', [
'uses' => 'FormController@displayImage',
]);

我正在搜索的网址就像

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

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

我花了很多时间寻找解决方案,我终于找到了! 只需在返回响应之前添加ob_end_clean,如下所示:

ob_end_clean();
return response()->file($path,['Content-type' => 'image/jpeg']);

我无法解释,因为我不知道发生了什么,任何人都能解释这一点?

你可以这样做

$storagePath = storage_path("app/taskImage/".$filename);
return Image::make($storagePath)->response();

最新更新