I need to be able to resize an image and put the resized version back into the $request
, does anyone know if thats possible?
Basically I have inherited some code that contains potentially 100+ separate file upload sections, and it is now my task to resize all images on the site if they are above a certain size.
So I now need to intercept ALL image uploads on the application, detect if they are above a set size and if they are, resize them.
All code i've found online only shows how to resize the image then save the resized version straight away, but I need to be able to resize the image then put it BACK into the $request
to be processed by the controller.
The images come in the form of arrays of images from separate sections, so i need to be able to loop the entire request, check if any of the inputs contain/are files, then if they are check the sizes of them. If they're above a set size, then resize them and replace them in the $request
so that when the request continues, the controller can process the image as normal but it will be processing the new resized version.
I have tried resizing images and then using laravels $request->merge()
method but I cannot get it to work.
At the moment I am resizing all images in a middleware, like this
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);
}
I just can't get it to work!
Is this possible?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire