Skip to content

Commit

Permalink
上传头像
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Mar 6, 2022
1 parent 10e3009 commit 9c8b73e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
41 changes: 41 additions & 0 deletions app/Handlers/ImageUploadHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Handlers;

use Illuminate\Support\Str;

class ImageUploadHandler
{
// 只允许以下后缀名的图片文件上传
protected $allowed_ext = ["png", "jpg", "gif", 'jpeg'];

public function save($file, $folder, $file_prefix)
{
// 构建存储的文件夹规则,值如:uploads/images/avatars/201709/21/
// 文件夹切割能让查找效率更高。
$folder_name = "uploads/images/$folder/" . date("Ym/d", time());

// 文件具体存储的物理路径,`public_path()` 获取的是 `public` 文件夹的物理路径。
// 值如:/home/vagrant/Code/larabbs/public/uploads/images/avatars/201709/21/
$upload_path = public_path() . '/' . $folder_name;

// 获取文件的后缀名,因图片从剪贴板里黏贴时后缀名为空,所以此处确保后缀一直存在
$extension = strtolower($file->getClientOriginalExtension()) ?: 'png';

// 拼接文件名,加前缀是为了增加辨析度,前缀可以是相关数据模型的 ID
// 值如:1_1493521050_7BVc9v9ujP.png
$filename = $file_prefix . '_' . time() . '_' . Str::random(10) . '.' . $extension;

// 如果上传的不是图片将终止操作
if ( ! in_array($extension, $this->allowed_ext)) {
return false;
}

// 将图片移动到我们的目标存储路径中
$file->move($upload_path, $filename);

return [
'path' => config('app.url') . "/$folder_name/$filename"
];
}
}
14 changes: 12 additions & 2 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Requests\UserRequest;
use App\Handlers\ImageUploadHandler;

class UsersController extends Controller
{
Expand All @@ -18,9 +19,18 @@ public function edit(User $user)
return view('users.edit', compact('user'));
}

public function update(UserRequest $request, User $user)
public function update(UserRequest $request, ImageUploadHandler $uploader, User $user)
{
$user->update($request->all());
$data = $request->all();

if ($request->avatar) {
$result = $uploader->save($request->avatar, 'avatars', $user->id);
if ($result) {
$data['avatar'] = $result['path'];
}
}

$user->update($data);
return redirect()->route('users.show', $user->id)->with('success', '个人资料更新成功!');
}
}
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class User extends Authenticatable implements MustVerifyEmail
'email',
'password',
'introduction',
'avatar',
];

protected $hidden = [
Expand Down
2 changes: 2 additions & 0 deletions public/uploads/images/avatars/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
14 changes: 13 additions & 1 deletion resources/views/users/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
</div>

<div class="card-body">
<form action="{{ route('users.update', $user->id) }}" method="POST" accept-charset="UTF-8">
<form action="{{ route('users.update', $user->id) }}" method="POST"
accept-charset="UTF-8"
enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

Expand All @@ -31,6 +33,16 @@
<label for="introduction-field">个人简介</label>
<textarea name="introduction" id="introduction-field" class="form-control" rows="3">{{ old('introduction', $user->introduction) }}</textarea>
</div>

<div class="mb-4">
<label for="" class="avatar-label form-label">用户头像</label>
<input type="file" name="avatar" class="form-control">
@if($user->avatar)
<br>
<img class="thumbnail img-responsive" src="{{ $user->avatar }}" width="200" />
@endif
</div>

<div class="well well-sm">
<button type="submit" class="btn btn-primary">保存</button>
</div>
Expand Down

0 comments on commit 9c8b73e

Please sign in to comment.