Last active
March 26, 2020 12:01
-
-
Save MrWeb/e3615d862480dc3801def77639c663f0 to your computer and use it in GitHub Desktop.
Upload immagine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Image as ImageModel; | |
use Intervention\Image\ImageManagerStatic as Image; | |
public function upload(Request $request) | |
{ | |
//Verifico Immagine | |
Validator::make(request()->all(), [ | |
'file' => 'image|mimes:jpeg,png,jpg,gif,svg|max:5120', | |
])->validate(); | |
ini_set('memory_limit', '256M'); | |
$uploadedImages = ImageModel::where('id_immobile', $request->estate_id); | |
//Estensione originale immagine | |
$extension = strtolower($request->file->getClientOriginalExtension()); | |
//Composizione nome immagine da upload {ID}_{branch}_{date}_{time}_{random} | |
$imageName = $request->estate_id . '_' . Auth::user()->branch_id . '_' . date('Ymd') . '_' . time() . '_' . Str::random(15); | |
$imageFile = $request->file; | |
$filename = $imageFile->getClientOriginalName(); | |
//Resize immagine | |
$imageResize = Image::make($imageFile->getRealPath()); | |
if ($imageResize->height() > 1050) { | |
$imageResize->resize(null, 1050, function ($constraint) { | |
$constraint->aspectRatio(); | |
}); | |
} | |
//Thumb immagine | |
$imageThumb = Image::make($imageFile->getRealPath()); | |
if ($imageThumb->height() > 150) { | |
$imageThumb->resize(null, 150, function ($constraint) { | |
$constraint->aspectRatio(); | |
}); | |
} | |
//Salvo immagine + thumb | |
$path = 'app/public/images/estates/' . Auth::user()->branch_id; | |
$saveOriginalResized = $imageResize->save(storage_path($path . '/' . $imageName . '.' . $extension)); | |
$saveOriginalThumb = $imageThumb->save(storage_path($path . '/' . $imageName . '_thumb.' . $extension)); | |
if ($saveOriginalResized && $saveOriginalThumb) { | |
$sequence = $uploadedImages->max('sequence'); | |
$image = new ImageModel; | |
$image->original_name = $filename; | |
$image->img_url = $imageName . '.' . $extension; | |
$image->save(); | |
return response()->json(['success' => $image]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment