From dfdddf54f8c253106dcdaa4effc499d4009b5ceb Mon Sep 17 00:00:00 2001 From: ScuffedNewt Date: Thu, 21 Nov 2024 18:20:59 +0000 Subject: [PATCH 1/2] feat: avatar cropping --- .../Controllers/Users/AccountController.php | 5 +- app/Services/UserService.php | 11 +++- resources/views/account/settings.blade.php | 64 ++++++++++++++++++- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Users/AccountController.php b/app/Http/Controllers/Users/AccountController.php index 7c249749f9..3e11ed6403 100644 --- a/app/Http/Controllers/Users/AccountController.php +++ b/app/Http/Controllers/Users/AccountController.php @@ -86,7 +86,10 @@ public function postProfile(Request $request) { * @return \Illuminate\Http\RedirectResponse */ public function postAvatar(Request $request, UserService $service) { - if ($service->updateAvatar($request->file('avatar'), Auth::user())) { + $data = $request->only([ + 'avatar', 'x0', 'x1', 'y0', 'y1', + ]); + if ($service->updateAvatar($data, Auth::user())) { flash('Avatar updated successfully.')->success(); } else { foreach ($service->errors()->getMessages()['error'] as $error) { diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 46d8a9d93a..1d24f06061 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -303,10 +303,11 @@ public function updateContentWarningVisibility($data, $user) { * * @return bool */ - public function updateAvatar($avatar, $user) { + public function updateAvatar($data, $user) { DB::beginTransaction(); try { + $avatar = $data['avatar']; if (!$avatar) { throw new \Exception('Please upload a file.'); } @@ -329,7 +330,13 @@ public function updateAvatar($avatar, $user) { throw new \Exception('Failed to move file.'); } } else { - if (!Image::make($avatar)->resize(150, 150)->save(public_path('images/avatars/'.$filename))) { + // crop image first + $cropWidth = $data['x1'] - $data['x0']; + $cropHeight = $data['y1'] - $data['y0']; + + $image = Image::make($avatar); + $image->crop($cropWidth, $cropHeight, $data['x0'], $data['y0']); + if (!$image->resize(150, 150)->save(public_path('images/avatars/'.$filename))) { throw new \Exception('Failed to process avatar.'); } } diff --git a/resources/views/account/settings.blade.php b/resources/views/account/settings.blade.php index c555bbb2d4..2c147831d3 100644 --- a/resources/views/account/settings.blade.php +++ b/resources/views/account/settings.blade.php @@ -17,9 +17,21 @@ @endif {!! Form::open(['url' => 'account/avatar', 'files' => true]) !!} +
+
+ + {!! Form::hidden('x0', null, ['id' => 'cropX0']) !!} + {!! Form::hidden('x1', null, ['id' => 'cropX1']) !!} + {!! Form::hidden('y0', null, ['id' => 'cropY0']) !!} + {!! Form::hidden('y1', null, ['id' => 'cropY1']) !!} +
+
+ Note: Cropping does not work on gifs. +
+
{!! Form::label('avatar', 'Update Profile Image', ['class' => 'custom-file-label']) !!} - {!! Form::file('avatar', ['class' => 'custom-file-input']) !!} + {!! Form::file('avatar', ['class' => 'custom-file-input', 'id' => 'avatar']) !!}
{!! Form::submit('Edit', ['class' => 'btn btn-primary']) !!} @@ -193,3 +205,53 @@ @endif
@endsection +@section('scripts') + +@endsection From bc236028ff428fcf45cfc7612eb2101e5d5e2e87 Mon Sep 17 00:00:00 2001 From: ScuffedNewt Date: Thu, 21 Nov 2024 18:25:46 +0000 Subject: [PATCH 2/2] refactor: fix PHP styling --- app/Services/UserService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 1d24f06061..081477af8c 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -299,7 +299,7 @@ public function updateContentWarningVisibility($data, $user) { * Updates the user's avatar. * * @param User $user - * @param mixed $avatar + * @param mixed $data * * @return bool */