gpt4 book ai didi

php - 如何使用 laravel 4 安静地返回图像?

转载 作者:行者123 更新时间:2023-12-04 21:05:20 24 4
gpt4 key购买 nike

我设置了一个服务器来为不同域上的网页提供服务(特别是移动设备或 localhost:9000,其中 laravel 在 localhost:8000 上提供服务)。我试图将这些页面上的图像请求返回到我的 laravel 服务器,但我遇到了问题。在论坛帖子中,我认为在请求上设置 header 可以解决问题,但是当我导航到/api/v1/images/default.jpg 时,没有显示默认的猫图像。相反,我得到一个没有图像的框。

现在,图像在我的公共(public)文件夹中,所以如果我浏览到/public/images/default.jpg 我确实看到了我的猫图像,但我宁愿在我的/api/v1/... 路由中提供图像.

Route::get('images/{imageName}', function($imageName){
$img = 'public/images/' . $imageName;
// return $img;
echo $img . "\n\n";
if(File::exists($img)) {
// return "true";
// return Response::make($img, 200, array('content-type' => 'image/jpg'));
// return Response::download($img, $imageName);
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: inline; filename=\"".$imageName."\"");
header("Content-Type: image/jpg");
header("Content-Transfer-Encoding: binary");
//stream the file out
readfile($img);
exit;
} else {
return "false";
}
return $img;
// return File::exists($img);
// return File::isFile('/images/' . $imageName);
// return $imageName;
// if(File::isFile('images/' + $imageName)){
// return Response::make('images/' + $imageName, 200, array('content-type' => 'image/jpg'));
// }
});

最佳答案

使用Response::stream 方法来做:

Route::get('/image', function () {

return Response::stream(function () {

$filename = '/path/to/your/image.jpg';

readfile($filename);

}, 200, ['content-type' => 'image/jpeg']);
});

关于php - 如何使用 laravel 4 安静地返回图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25277542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com