Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avatar cropping #1132

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/Http/Controllers/Users/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 10 additions & 3 deletions app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,15 @@ public function updateContentWarningVisibility($data, $user) {
* Updates the user's avatar.
*
* @param User $user
* @param mixed $avatar
* @param mixed $data
*
* @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.');
}
Expand All @@ -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.');
}
}
Expand Down
64 changes: 63 additions & 1 deletion resources/views/account/settings.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@
</div>
@endif
{!! Form::open(['url' => 'account/avatar', 'files' => true]) !!}
<div class="card mb-3 hide" id="avatarCrop">
<div class="card-body">
<img src="#" id="cropper" class="hide" alt="" />
{!! Form::hidden('x0', null, ['id' => 'cropX0']) !!}
{!! Form::hidden('x1', null, ['id' => 'cropX1']) !!}
{!! Form::hidden('y0', null, ['id' => 'cropY0']) !!}
{!! Form::hidden('y1', null, ['id' => 'cropY1']) !!}
</div>
<div class="alert alert-info mx-3">
<b>Note:</b> Cropping does not work on gifs.
</div>
</div>
<div class="custom-file mb-1">
{!! 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']) !!}
</div>
<div class="text-right">
{!! Form::submit('Edit', ['class' => 'btn btn-primary']) !!}
Expand Down Expand Up @@ -193,3 +205,53 @@
@endif
</div>
@endsection
@section('scripts')
<script>
var $avatarCrop = $('#avatarCrop');
var $cropper = $('#cropper');
var c = null;
var $x0 = $('#cropX0');
var $y0 = $('#cropY0');
var $x1 = $('#cropX1');
var $y1 = $('#cropY1');
var zoom = 0;

function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$cropper.attr('src', e.target.result);
c = new Croppie($cropper[0], {
viewport: {
width: 200,
height: 200,
},
boundary: {
width: 250,
height: 250
},
update: function() {
updateCropValues();
}
});
updateCropValues();
$avatarCrop.removeClass('hide');
$cropper.removeClass('hide');
}
reader.readAsDataURL(input.files[0]);
}
}

$("#avatar").change(function() {
readURL(this);
});

function updateCropValues() {
var values = c.get();
$x0.val(values.points[0]);
$y0.val(values.points[1]);
$x1.val(values.points[2]);
$y1.val(values.points[3]);
}
</script>
@endsection