【发布时间】:2018-12-26 08:32:17
【问题描述】:
我在我的 Lumen 项目中使用intervention image 并且一切正常,直到我遇到将编码图像作为可下载响应,该响应在表单提交时包含将被格式化为特定格式的图像文件,例如webp, jpg, png 将作为可下载文件发回给用户,下面是我的尝试。
public function image_format(Request $request){
$this->validate($request, [
'image' => 'required|file',
]);
$raw_img = $request->file('image');
$q = (int)$request->input('quality',100);
$f = $request->input('format','jpg');
$img = Image::make($raw_img->getRealPath())->encode('webp',$q);
header('Content-Type: image/webp');
echo $img;
}
但不幸的是,这不是我预期的输出,它只是显示了图像。
从此post,我使用代码并尝试实现我的目标
public function image_format(Request $request){
$this->validate($request, [
'image' => 'required|file',
]);
$raw_img = $request->file('image');
$q = (int)$request->input('quality',100);
$f = $request->input('format','jpg');
$img = Image::make($raw_img->getRealPath())->encode('webp',$q);
$headers = [
'Content-Type' => 'image/webp',
'Content-Disposition' => 'attachment; filename='. $raw_img->getClientOriginalName().'.webp',
];
$response = new BinaryFileResponse($img, 200 , $headers);
return $response;
}
但它不起作用,而是向我显示了这个错误
有什么建议吗?
【问题讨论】:
-
只是出于好奇,您为什么要下载您正在上传的图片?
-
@user3783243 image.intervention.io/api/encode
-
@RossWilson 只是一个用于将图像转换为用户所需格式的 api
-
@RossWilson 很抱歉,我正在使用 Lumen
标签: php laravel lumen intervention