Keep uploaded avatar images within the same aspect ratio #309

This commit is contained in:
Nabeel Shahzad
2019-05-13 07:52:07 -05:00
parent defd78e5c2
commit e1bdef98d6

View File

@@ -155,7 +155,20 @@ class ProfileController extends Controller
$avatar = $request->file('avatar');
$file_name = $user->pilot_id.'.'.$avatar->getClientOriginalExtension();
$path = "avatars/{$file_name}";
Image::make($avatar)->resize(config('phpvms.avatar.width'), config('phpvms.avatar.height'))->save(public_path('uploads/avatars/'.$file_name));
// Create the avatar, resizing it and keeping the aspect ratio.
// https://stackoverflow.com/a/26892028
$w = config('phpvms.avatar.width');
$h = config('phpvms.avatar.height');
$canvas = Image::canvas($w, $h);
$image = Image::make($avatar)->resize($w, $h, static function ($constraint) {
$constraint->aspectRatio();
});
$canvas->insert($image);
$canvas->save(public_path('uploads/avatars/'.$file_name));
$req_data['avatar'] = $path;
}