调整大小并替换 Laravel 请求中的图像
Resize and replace image in Laravel request
我需要能够调整图像大小并将调整后的版本放回 $request
,有人知道这是否可行吗?
基本上我继承了一些代码,其中可能包含 100 多个单独的文件上传部分,现在我的任务是调整网站上所有超过特定大小的图像的大小。
所以我现在需要拦截应用程序上的所有图像上传,检测它们是否超过设定大小,如果超过,则调整它们的大小。
我在网上找到的所有代码只展示了如何调整图像大小然后立即保存调整后的版本,但我需要能够调整图像大小然后将其放回 $request
由控制器处理。
图像以来自不同部分的图像数组的形式出现,所以我需要能够循环整个请求,检查是否有任何输入 contain/are 文件,然后检查它们是否他们的大小。如果它们超过设定的大小,则调整它们的大小并在 $request
中替换它们,以便当请求继续时,控制器可以正常处理图像,但它将处理新的调整大小的版本。
我试过调整图像大小,然后使用 laravels $request->merge()
方法,但我无法让它工作。
目前我正在调整中间件中所有图像的大小,就像这样
public function handle($request, Closure $next)
{
foreach($request->files as $fileKey => $file){
//Create a new array to add the newly sized images in
$newFileArray = [];
//Get each of the files that are being uploaded in the request, if there are no files this will just be ignored.
foreach ($file as $key => $f) {
if(!is_null($f)){
$image = Image::make($f);
if($image->height() > 500 || $image->width() > 500){
$image->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
});
}
$newFileArray[$key] = $image;
} else {
$newFileArray[$key] = null;
}
}
$request->merge([
$fileKey => $newFileArray
]);
};
return $next($request);
}
我无法让它工作!
这可能吗?
编辑
在以下答案之一的评论中提出了很好的建议后,我通过直接编辑临时图像文件实现了这一点,这样我就不必弄乱请求了,我就是这样做的它。
public function handle($request, Closure $next)
{
foreach($request->files as $fileKey => $file){
//Get each of the files that are being uploaded in the request, if there are no files this will just be ignored.
foreach ($file as $key => $f) {
if(!is_null($f)){
$image = Image::make($f->getPathName());
if($image->height() > 500 || $image->width() > 500){
$image->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
});
$image->save($f->getPathName());
}
}
}
};
return $next($request);
}
我刚读到 Laravel 使用 PSR-7 请求。
https://laravel.com/docs/5.7/requests#psr7-requests
这些是不可变的。换句话说,数据一旦设置就无法更改。但是,您可以 做的是使用新参数创建新请求。
查看 PSR-7 接口,我们看到有一个方法看起来正是您需要的:
https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php#L150
/**
* Create a new instance with the specified uploaded files.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* updated body parameters.
*
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
* @return static
* @throws \InvalidArgumentException if an invalid structure is provided.
*/
public function withUploadedFiles(array $uploadedFiles);
所以,做你的事,创建你的数组,一旦准备就绪,像这样替换你的请求:
$request = $request->withUploadedFiles($yourNewArray);
我需要能够调整图像大小并将调整后的版本放回 $request
,有人知道这是否可行吗?
基本上我继承了一些代码,其中可能包含 100 多个单独的文件上传部分,现在我的任务是调整网站上所有超过特定大小的图像的大小。
所以我现在需要拦截应用程序上的所有图像上传,检测它们是否超过设定大小,如果超过,则调整它们的大小。
我在网上找到的所有代码只展示了如何调整图像大小然后立即保存调整后的版本,但我需要能够调整图像大小然后将其放回 $request
由控制器处理。
图像以来自不同部分的图像数组的形式出现,所以我需要能够循环整个请求,检查是否有任何输入 contain/are 文件,然后检查它们是否他们的大小。如果它们超过设定的大小,则调整它们的大小并在 $request
中替换它们,以便当请求继续时,控制器可以正常处理图像,但它将处理新的调整大小的版本。
我试过调整图像大小,然后使用 laravels $request->merge()
方法,但我无法让它工作。
目前我正在调整中间件中所有图像的大小,就像这样
public function handle($request, Closure $next)
{
foreach($request->files as $fileKey => $file){
//Create a new array to add the newly sized images in
$newFileArray = [];
//Get each of the files that are being uploaded in the request, if there are no files this will just be ignored.
foreach ($file as $key => $f) {
if(!is_null($f)){
$image = Image::make($f);
if($image->height() > 500 || $image->width() > 500){
$image->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
});
}
$newFileArray[$key] = $image;
} else {
$newFileArray[$key] = null;
}
}
$request->merge([
$fileKey => $newFileArray
]);
};
return $next($request);
}
我无法让它工作!
这可能吗?
编辑
在以下答案之一的评论中提出了很好的建议后,我通过直接编辑临时图像文件实现了这一点,这样我就不必弄乱请求了,我就是这样做的它。
public function handle($request, Closure $next)
{
foreach($request->files as $fileKey => $file){
//Get each of the files that are being uploaded in the request, if there are no files this will just be ignored.
foreach ($file as $key => $f) {
if(!is_null($f)){
$image = Image::make($f->getPathName());
if($image->height() > 500 || $image->width() > 500){
$image->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
});
$image->save($f->getPathName());
}
}
}
};
return $next($request);
}
我刚读到 Laravel 使用 PSR-7 请求。
https://laravel.com/docs/5.7/requests#psr7-requests
这些是不可变的。换句话说,数据一旦设置就无法更改。但是,您可以 做的是使用新参数创建新请求。
查看 PSR-7 接口,我们看到有一个方法看起来正是您需要的:
https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php#L150
/**
* Create a new instance with the specified uploaded files.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* updated body parameters.
*
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
* @return static
* @throws \InvalidArgumentException if an invalid structure is provided.
*/
public function withUploadedFiles(array $uploadedFiles);
所以,做你的事,创建你的数组,一旦准备就绪,像这样替换你的请求:
$request = $request->withUploadedFiles($yourNewArray);