public function uploadImage($file, $width = 1440, $height = null)
{
if (!is_object($file)) {
return '';
}
$allowed_extensions = ["png", "jpg", "gif"];
if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
return ['error' => 'You may only upload png, jpg or gif.'];
}
$fileName = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension() ?: 'png';
$folderName = rtrim(config('custom.uploads.images'), '/') . '/' . date("Ym", time()) . '/' . date("d", time());
$destinationPath = public_path() . '/' . $folderName;
$safeNameWithoutExt = str_random(10);
$safeName = $safeNameWithoutExt . '.' . $extension;
$file->move($destinationPath, $safeName);
// If is not gif file, we will try to reduse the file size
if ($file->getClientOriginalExtension() != 'gif') {
// open an image file
// $img = Image::make($destinationPath . '/' . $safeName);
// // prevent possible upsizing
// $img->resize($width, $height, function ($constraint) {
// $constraint->aspectRatio();
// $constraint->upsize();
// });
// // finally we save the image as a new file
// $img->save();
//save to db
Image::create(['image_name' => $safeNameWithoutExt, 'image_path' => $folderName . '/' . $safeName, 'user_id' => Auth::user()->id]);
}
return ['origin_name' => $fileName, 'extension' => $extension, 'image_path' => $folderName . '/' . $safeName];
}