首页 > 解决方案 > 使用干预将图像转换为灰度

问题描述

我正在尝试将我的图像转换为灰度,然后在Laravel中下载它,但每次我都收到此错误

文件“”不存在

不知道为什么它在这里给出这个错误是我的代码。

$file = public_path() . "/large/s/" . $sheet[0]->sheet_f_id . '-s.jpg';
$image = Image::make($file);
$grayScale = $image->greyscale();
return Response::download($grayScale);

当我转储$file变量时,我得到了类似这样的响应。

“D:\xampp\htdocs\wikistaging\public/large/s/03-02-05-025-s.jpg”

但它仍然给了我山姆错误,为什么会发生这种情况。任何帮助都会很棒。

标签: phplaravel-5grayscaleintervention

解决方案


如果要下载,则首先必须保存创建的文件并需要提供下载路径。这是工作示例

$img_name=$sheet[0]->sheet_f_id . '-s.jpg';
$destination_path=public_path() . "/large/s/";

$file = $destination_path.$img_name;
$image = Image::make($file);

$image->greyscale()->save($destination_path.'gray-'.$img_name);
return Response::download($destination_path.'gray_'.$img_name);

如果您不想保留可以删除的文件,请将最后一行替换为以下行。

return Response::download($destination_path.'gray_'.$img_name)->deleteFileAfterSend(true);

希望它对你有用。


推荐阅读