在浏览器中显示之前调整图像大小
Resizing an image before displaying it in the browser
我正在创建一个 API,其中我有一个名为 OnlineAds()
的模型,它有这些列 title
、full_discription
、short_discription
和image
,我还创建了一个输入表单来填写这些字段并上传图片,在 image
字段中我添加了图片的名称并将图片存储在这个路径中:storage/uploads
。我能够检索图像并显示它,但我仍然不知道如何在显示之前调整它的大小(假设我想要它是 300 x 300),谁能告诉我怎么做?
注意:我尝试使用 Stapler 和 Imagine,但它们似乎存在多个错误并失败了!
这是我的控制器:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$filetype = File::type($path);
$response = Response::make(File::get($path), 200);
$response->header('Content-Type', $filetype);
return $response;
}
}
您可以考虑使用 Intervention 来处理图像以及处理响应。它可以像这样简单:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$img = Image::make($path);
$img->resize(300, 300);
return $img->response();
}
}
detailed instructions 介绍了如何安装 Intervention 库并将其与 Laravel 集成,安装起来应该不会超过几分钟 运行 .
我正在创建一个 API,其中我有一个名为 OnlineAds()
的模型,它有这些列 title
、full_discription
、short_discription
和image
,我还创建了一个输入表单来填写这些字段并上传图片,在 image
字段中我添加了图片的名称并将图片存储在这个路径中:storage/uploads
。我能够检索图像并显示它,但我仍然不知道如何在显示之前调整它的大小(假设我想要它是 300 x 300),谁能告诉我怎么做?
注意:我尝试使用 Stapler 和 Imagine,但它们似乎存在多个错误并失败了!
这是我的控制器:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$filetype = File::type($path);
$response = Response::make(File::get($path), 200);
$response->header('Content-Type', $filetype);
return $response;
}
}
您可以考虑使用 Intervention 来处理图像以及处理响应。它可以像这样简单:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$img = Image::make($path);
$img->resize(300, 300);
return $img->response();
}
}
detailed instructions 介绍了如何安装 Intervention 库并将其与 Laravel 集成,安装起来应该不会超过几分钟 运行 .