2017-12-30 20:23:09 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Http\Requests\UserUpdate;
|
|
|
|
|
use App\Models\User;
|
2017-12-31 04:50:35 +00:00
|
|
|
|
use Intervention\Image\Facades\Image;
|
2017-12-30 20:23:09 +00:00
|
|
|
|
|
|
|
|
|
class UsersController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function show(User $user)
|
|
|
|
|
{
|
|
|
|
|
return view('users.show', compact('user'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(User $user)
|
|
|
|
|
{
|
2017-12-31 04:50:35 +00:00
|
|
|
|
$this->authorize('update', $user);
|
2017-12-30 20:23:09 +00:00
|
|
|
|
return view('users.edit', compact('user'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(User $user, UserUpdate $request)
|
|
|
|
|
{
|
2017-12-31 04:50:35 +00:00
|
|
|
|
$this->authorize('update', $user);
|
2017-12-30 20:23:09 +00:00
|
|
|
|
$attribute = $request->only(['name', 'email', 'introduction']);
|
2017-12-31 04:50:35 +00:00
|
|
|
|
if ($image = $request->file('avatar')) {
|
|
|
|
|
$user->avatar = $image->store('public/avatar');
|
|
|
|
|
$file = Image::make(storage_path('app/' . $user->avatar));
|
|
|
|
|
$file->resize(1280, null, function ($constraint) {
|
|
|
|
|
|
|
|
|
|
// 设定宽度是 $max_width,高度等比例双方缩放
|
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
|
|
|
|
|
|
// 防止裁图时图片尺寸变大
|
|
|
|
|
$constraint->upsize();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 对图片修改后进行保存
|
|
|
|
|
$file->save();
|
|
|
|
|
|
2017-12-30 20:23:09 +00:00
|
|
|
|
}
|
|
|
|
|
$user->update($attribute);
|
|
|
|
|
return redirect()->route('users.show', [$user])->with('success', '更新成功!');
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|