Good morning,
I would like to reduce the size of the image before saving it in the storage folder
Below is the code of my function
thank you for your support
public static function pre_update($Model, $data, $id)
{
$m = $Model::find($id);
if ($m) {
$field = $m->getFillable();
$name = explode("App\", $Model)[1];
foreach ($data as $key => $value) {
if (in_array($key, $field)) {
if (in_array($key, $m->getFiles())) {
$fop =$m->getOriginal()[$key];
if (Storage::has($fop))
Storage::delete($fop);
$image = $value;
$fpath = "img/" . strtolower($name) . "/" . uniqid() . '_' . $image->getClientOriginalName();
$m->$key = $fpath;
Storage::put($fpath, File::get($image));
} else {
$m->$key = $value;
}
}
}
return $m;
} else {
return null;
}
}
2
Answers
You should use package Intervention
It will help you reduce the size and dimensions of the photos and make storage easier
Here is the documentation link
intervention
You can use the library Imagine.
Install wia composer "composer require imagine/imagine"
It should look like this: