diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index a8b0cd0..edb0d45 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Http\Requests\UserUpdate; use App\Models\User; +use Intervention\Image\Facades\Image; class UsersController extends Controller { @@ -14,14 +15,29 @@ class UsersController extends Controller public function edit(User $user) { + $this->authorize('update', $user); return view('users.edit', compact('user')); } public function update(User $user, UserUpdate $request) { + $this->authorize('update', $user); $attribute = $request->only(['name', 'email', 'introduction']); - if ($request->file('avatar')) { - $user->avatar = $request->file('avatar')->store('public/avatar'); + 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(); + } $user->update($attribute); return redirect()->route('users.show', [$user])->with('success', '更新成功!'); diff --git a/app/Http/Requests/UserUpdate.php b/app/Http/Requests/UserUpdate.php index 3569ebf..fc2a1e5 100644 --- a/app/Http/Requests/UserUpdate.php +++ b/app/Http/Requests/UserUpdate.php @@ -31,7 +31,7 @@ class UserUpdate extends FormRequest ], 'email' => ['required', 'unique:users,email,' . Auth::id()], 'introduction' => 'nullable|max:255', - 'avatar' => 'image|nullable', + 'avatar' => 'image|nullable|dimensions:min_width=200,min_height=200', ]; } @@ -40,6 +40,7 @@ class UserUpdate extends FormRequest { return [ 'introduction' => '简介', + 'avatar' => '头像' ]; } } diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php new file mode 100644 index 0000000..bd40a50 --- /dev/null +++ b/app/Models/BaseModel.php @@ -0,0 +1,17 @@ +id === $user->id; + } + +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 9e68caa..687c65c 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Models\User; +use App\Policies\UserPolicy; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider @@ -13,6 +15,7 @@ class AuthServiceProvider extends ServiceProvider */ protected $policies = [ 'App\Model' => 'App\Policies\ModelPolicy', + User::class => UserPolicy::class, ]; /** diff --git a/app/Tools/ImageUploadTool.php b/app/Tools/ImageUploadTool.php index a7c6d1b..e34436d 100644 --- a/app/Tools/ImageUploadTool.php +++ b/app/Tools/ImageUploadTool.php @@ -10,6 +10,7 @@ namespace App\Tools; use Illuminate\Http\File; +use Intervention\Image\Facades\Image; class ImageUploadTool { @@ -21,9 +22,10 @@ class ImageUploadTool * @param $file File * @param $folder * @param $file_prefix + * @param bool $max_width * @return array|bool */ - public function save($file, $folder, $file_prefix) + public function save($file, $folder, $file_prefix, $max_width = false) { // 构建存储的文件夹规则,值如:uploads/images/avatars/201709/21/ // 文件夹切割能让查找效率更高。 @@ -48,8 +50,34 @@ class ImageUploadTool // 将图片移动到我们的目标存储路径中 $file->move($upload_path, $filename); + // 如果限制了图片宽度,就进行裁剪 + if ($max_width && $extension != 'gif') { + + // 此类中封装的函数,用于裁剪图片 + $this->reduceSize($upload_path . '/' . $filename, $max_width); + } + return [ 'path' => config('app.url') . "/$folder_name/$filename" ]; } + + public function reduceSize($file_path, $max_width) + { + // 先实例化,传参是文件的磁盘物理路径 + $image = Image::make($file_path); + + // 进行大小调整的操作 + $image->resize($max_width, null, function ($constraint) { + + // 设定宽度是 $max_width,高度等比例双方缩放 + $constraint->aspectRatio(); + + // 防止裁图时图片尺寸变大 + $constraint->upsize(); + }); + + // 对图片修改后进行保存 + $image->save(); + } } \ No newline at end of file diff --git a/database/migrations/2017_12_31_124132_create_categories_table.php b/database/migrations/2017_12_31_124132_create_categories_table.php new file mode 100644 index 0000000..60203e3 --- /dev/null +++ b/database/migrations/2017_12_31_124132_create_categories_table.php @@ -0,0 +1,34 @@ +increments('id'); + $table->string('name')->index()->comment('名称'); + $table->text('description')->nullable()->comment('描述'); + $table->integer('post_count')->default(0)->comment('帖子数'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('categories'); + } +} diff --git a/database/migrations/2017_12_31_124634_seed_categories_data.php b/database/migrations/2017_12_31_124634_seed_categories_data.php new file mode 100644 index 0000000..1e21e98 --- /dev/null +++ b/database/migrations/2017_12_31_124634_seed_categories_data.php @@ -0,0 +1,44 @@ + '分享', + 'description' => '分享创造,分享发现', + ], + [ + 'name' => '教程', + 'description' => '开发技巧、推荐扩展包等', + ], + [ + 'name' => '问答', + 'description' => '请保持友善,互帮互助', + ], + [ + 'name' => '公告', + 'description' => '站点公告', + ], + ]; + \Illuminate\Support\Facades\DB::table('categories')->insert($categories); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + \Illuminate\Support\Facades\DB::table('categories')->truncate(); + } +} diff --git a/resources/views/layouts/_header.blade.php b/resources/views/layouts/_header.blade.php index 1f35477..3adecb7 100644 --- a/resources/views/layouts/_header.blade.php +++ b/resources/views/layouts/_header.blade.php @@ -34,7 +34,7 @@ diff --git a/routes/web.php b/routes/web.php index ed9a9b8..91a0480 100644 --- a/routes/web.php +++ b/routes/web.php @@ -15,7 +15,8 @@ use Illuminate\Support\Facades\Route; Route::get('/', 'PagesController@root')->name('home'); -Route::resource('users', 'UsersController', ['only' => ['update', 'show', 'edit']]); +Route::resource('users', 'UsersController', ['only' => ['update', 'show', 'edit']]) + ->middleware('auth'); // Authentication Routes... Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');