Use the imageHandler for profile avatar changes

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2023-01-21 18:05:20 -08:00
parent 3674f46ce0
commit f77d871dff

View file

@ -67,37 +67,9 @@ class ProfileController extends Controller
$user->location_id = $request->input('location_id'); $user->location_id = $request->input('location_id');
} }
// Handle the avatar upload and/or delete if necessary
app('\App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'avatar', 'avatars', 'avatar');
if ($request->input('avatar_delete') == 1) {
$user->avatar = null;
}
if ($request->hasFile('avatar')) {
$path = 'avatars';
if (! Storage::disk('public')->exists($path)) {
Storage::disk('public')->makeDirectory($path, 775);
}
$upload = $image = $request->file('avatar');
$ext = $image->getClientOriginalExtension();
$file_name = 'avatar-'.str_random(18).'.'.$ext;
if ($image->getClientOriginalExtension() != 'svg') {
$upload = Image::make($image->getRealPath())->resize(84, 84);
}
// This requires a string instead of an object, so we use ($string)
Storage::disk('public')->put($path.'/'.$file_name, (string) $upload->encode());
// Remove Current image if exists
if (($user->avatar) && (Storage::disk('public')->exists($path.'/'.$user->avatar))) {
Storage::disk('public')->delete($path.'/'.$user->avatar);
}
$user->avatar = $file_name;
}
if ($user->save()) { if ($user->save()) {
return redirect()->route('profile')->with('success', 'Account successfully updated'); return redirect()->route('profile')->with('success', 'Account successfully updated');