Skip to content

Commit

Permalink
更新:优化文件、图片上传处理器
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchunshu committed Aug 19, 2023
1 parent 9ce753c commit e0e25dd
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 66 deletions.
21 changes: 15 additions & 6 deletions app/Plugins/Core/src/Handler/AvatarUpload.php
@@ -1,6 +1,6 @@
<?php

declare (strict_types=1);
declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
Expand All @@ -14,18 +14,25 @@
use App\Plugins\User\src\Models\UserUpload;
use Illuminate\Support\Str;
use Intervention\Image\ImageManagerStatic as Image;

class AvatarUpload
{
public function save($file, $folder, $file_prefix = null, $max_width = false) : array
public function save($file, $folder, $file_prefix = null, $max_width = false): array
{
if (!auth()->check() && !admin_auth()->Check()) {
if (! auth()->check() && ! admin_auth()->Check()) {
return ['path' => '/404.jpg', 'success' => false, 'status' => '上传失败:未登录'];
}
if (!$file_prefix) {
// 获取上传的文件大小
$file_size = $file->getSize() / 1024;
if ((float) get_options('core_user_up_img_size', 2048) < $file_size) {
return ['path' => '/404.jpg', 'success' => false, 'status' => '上传失败,上传文件大小超过限制'];
}

if (! $file_prefix) {
$file_prefix = Str::random();
}
$folder_name = "upload/{$folder}/" . date('Ym/d', time());
if (!is_dir(public_path($folder_name))) {
if (! is_dir(public_path($folder_name))) {
mkdir(public_path($folder_name), 0777, true);
}
$upload_path = public_path() . '/' . $folder_name;
Expand Down Expand Up @@ -60,6 +67,7 @@ public function save($file, $folder, $file_prefix = null, $max_width = false) :
}
return ['path' => $upload['url'], 'raw_path' => $upload['path'], 'success' => $upload['success'], 'status' => '上传成功!'];
}

public function reduceSize($file_path, $max_width)
{
// 先实例化,传参是文件的磁盘物理路径
Expand All @@ -74,11 +82,12 @@ public function reduceSize($file_path, $max_width)
// 对图片修改后进行保存
$image->save();
}

private function webp($from, $to)
{
$image = Image::make($from);
$image->encode('webp');
$image->save($to);
unlink($from);
}
}
}
17 changes: 13 additions & 4 deletions app/Plugins/Core/src/Handler/FileUpload.php
@@ -1,6 +1,6 @@
<?php

declare (strict_types=1);
declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
Expand All @@ -12,13 +12,22 @@

use App\Plugins\Core\src\Service\FileStoreService;
use App\Plugins\User\src\Models\UserUpload;
use Hyperf\HttpMessage\Upload\UploadedFile;

class FileUpload
{
public function save($file, $folder, $file_prefix = null) : array
public function save(UploadedFile $file, $folder, $file_prefix = null): array
{
if (!auth()->check() && !admin_auth()->Check()) {
if (! auth()->check() && ! admin_auth()->Check()) {
return ['path' => '/404.jpg', 'success' => false, 'status' => '上传失败:未登录'];
}
if (auth()->check()) {
// 获取上传的文件大小
$file_size = $file->getSize() / 1024;
if ((float) get_options('core_user_up_file_size', 4096) < $file_size) {
return ['path' => '/404.jpg', 'success' => false, 'status' => '上传失败,上传文件大小超过限制'];
}
}
$service = new FileStoreService();
$upload = $service->save($file, $folder, $file_prefix);
if ($upload['success'] !== true) {
Expand All @@ -31,4 +40,4 @@ public function save($file, $folder, $file_prefix = null) : array
}
return ['path' => $upload['url'], 'raw_path' => $upload['path'], 'success' => $upload['success'], 'status' => '上传成功!'];
}
}
}
22 changes: 16 additions & 6 deletions app/Plugins/Core/src/Handler/UploadHandler.php
@@ -1,6 +1,6 @@
<?php

declare (strict_types=1);
declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
Expand All @@ -13,21 +13,29 @@
use App\Plugins\Core\src\Event\UploadImage;
use App\Plugins\Core\src\Service\FileStoreService;
use App\Plugins\User\src\Models\UserUpload;
use Hyperf\HttpMessage\Upload\UploadedFile;
use Illuminate\Support\Str;
use Intervention\Image\ImageManagerStatic as Image;

// 上传图片
class UploadHandler
{
public function save($file, $folder, $file_prefix = null, $max_width = 1500) : array
public function save(UploadedFile $file, $folder, $file_prefix = null, $max_width = 1500): array
{
if (!auth()->check() && !admin_auth()->Check()) {
if (! auth()->check() && ! admin_auth()->Check()) {
return ['path' => '/404.jpg', 'success' => false, 'status' => '上传失败:未登录'];
}
if (!$file_prefix) {
// 获取上传的文件大小
$file_size = $file->getSize() / 1024;
if ((float) get_options('core_user_up_img_size', 2048) < $file_size) {
return ['path' => '/404.jpg', 'success' => false, 'status' => '上传失败,上传文件大小超过限制'];
}

if (! $file_prefix) {
$file_prefix = Str::random();
}
$folder_name = "upload/{$folder}/" . date('Ym/d', time());
if (!is_dir(public_path($folder_name))) {
if (! is_dir(public_path($folder_name))) {
mkdir(public_path($folder_name), 0777, true);
}
$upload_path = public_path() . '/' . $folder_name;
Expand Down Expand Up @@ -58,6 +66,7 @@ public function save($file, $folder, $file_prefix = null, $max_width = 1500) : a
}
return ['path' => $upload['url'], 'raw_path' => $upload['path'], 'success' => $upload['success'], 'status' => '上传成功!'];
}

public function reduceSize($file_path, $max_width)
{
// 先实例化,传参是文件的磁盘物理路径
Expand All @@ -72,11 +81,12 @@ public function reduceSize($file_path, $max_width)
// 对图片修改后进行保存
$image->save();
}

private function webp($from, $to)
{
$image = Image::make($from);
$image->encode('webp');
$image->save($to);
unlink($from);
}
}
}

0 comments on commit e0e25dd

Please sign in to comment.