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 5ba481b commit 9c26b35
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
32 changes: 29 additions & 3 deletions app/Handlers/ImageUploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace App\Handlers;

use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Str;

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

public function save($file, $folder, $file_prefix)
public function save($file, $folder, $file_prefix, $max_width = false)
{
// 构建存储的文件夹规则,值如:uploads/images/avatars/201709/21/
// 文件夹切割能让查找效率更高。
Expand All @@ -34,8 +34,34 @@ public function save($file, $folder, $file_prefix)
// 将图片移动到我们的目标存储路径中
$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();
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function update(UserRequest $request, ImageUploadHandler $uploader, User
$data = $request->all();

if ($request->avatar) {
$result = $uploader->save($request->avatar, 'avatars', $user->id);
$result = $uploader->save($request->avatar, 'avatars', $user->id, 416);
if ($result) {
$data['avatar'] = $result['path'];
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"require": {
"php": "^8.0.2",
"guzzlehttp/guzzle": "^7.2",
"intervention/image": "^2.7",
"laravel/framework": "^9.2",
"laravel/sanctum": "^2.14.1",
"laravel/tinker": "^2.7",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions config/image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| Intervention Image supports "GD Library" and "Imagick" to process images
| internally. You may choose one of them according to your PHP
| configuration. By default PHP's "GD Library" implementation is used.
|
| Supported: "gd", "imagick"
|
*/

'driver' => 'gd'

];

0 comments on commit 9c26b35

Please sign in to comment.