refactor: 调整图片上传
This commit is contained in:
@@ -3,77 +3,10 @@ declare (strict_types = 1);
|
|||||||
|
|
||||||
namespace app\admin\controller\v1;
|
namespace app\admin\controller\v1;
|
||||||
|
|
||||||
use app\admin\model\v1\ImageModel;
|
|
||||||
use Intervention\Image\ImageManager;
|
|
||||||
use think\facade\Filesystem;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 图片管理控制器
|
* 图片管理控制器
|
||||||
*/
|
*/
|
||||||
class Images
|
class Images
|
||||||
{
|
{
|
||||||
// 上传
|
|
||||||
public function upload()
|
|
||||||
{
|
|
||||||
$param = request()->param(['module' => 'unknown']);
|
|
||||||
if (is_null($param)) {
|
|
||||||
return error('请确定请求参数正确');
|
|
||||||
}
|
|
||||||
$file = request()->file('image');
|
|
||||||
if (is_null($file)) {
|
|
||||||
return error('请确定上传对象或字段是否正确');
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$validate = validate([
|
|
||||||
'module' => 'require|max:64',
|
|
||||||
'image' => 'fileSize:1048576|fileExt:jpg,jpeg,png,gif'
|
|
||||||
]);
|
|
||||||
if (!$validate->check(['module' => $param['module'], 'image' => $file])) {
|
|
||||||
return error($validate->getError());
|
|
||||||
}
|
|
||||||
|
|
||||||
$storage = config('filesystem.disks.image.url');
|
|
||||||
|
|
||||||
$filemd5 = $file->md5();
|
|
||||||
$filesha1 = $file->sha1();
|
|
||||||
|
|
||||||
$image_model = ImageModel::md5($filemd5)->find();
|
|
||||||
if (is_null($image_model)) {
|
|
||||||
$filename = Filesystem::disk('image')->putFile($param['module'], $file);
|
|
||||||
// 生成缩略图
|
|
||||||
$image_manager = new ImageManager(new \Intervention\Image\Drivers\Gd\Driver());
|
|
||||||
$image = $image_manager->read('.' . $storage . '/' . $filename);
|
|
||||||
$image->scale(200, 200);
|
|
||||||
$idx = strrpos($filename, '.');
|
|
||||||
$thumb_filename = mb_substr($filename, 0, $idx) . '_thumb.' . mb_substr($filename, $idx + 1);
|
|
||||||
$image->save('.' . $storage . '/' . $thumb_filename);
|
|
||||||
|
|
||||||
// 保存图片
|
|
||||||
$image_model = new ImageModel();
|
|
||||||
$image_model->language_id = request()->lang_id;
|
|
||||||
$image_model->module = $param['module'];
|
|
||||||
$image_model->image_path = $filename;
|
|
||||||
$image_model->image_thumb = $thumb_filename;
|
|
||||||
$image_model->image_size = $file->getSize();
|
|
||||||
$image_model->image_type = $file->getOriginalMime();
|
|
||||||
$image_model->image_md5 = $filemd5;
|
|
||||||
$image_model->image_sha1 = $filesha1;
|
|
||||||
if (!$image_model->save()) {
|
|
||||||
return error('上传失败');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return success('操作成功', [
|
|
||||||
'url' => $storage . '/' . $image_model->image_path,
|
|
||||||
'thumb_url' => $storage . '/' . $image_model->image_thumb,
|
|
||||||
'filemd5' => $image_model->image_md5,
|
|
||||||
'filesha1' => $image_model->image_sha1
|
|
||||||
]);
|
|
||||||
} catch (\Throwable $th) {
|
|
||||||
return error($th->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return error('上传失败');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
79
app/admin/controller/v1/Upload.php
Normal file
79
app/admin/controller/v1/Upload.php
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\admin\controller\v1;
|
||||||
|
|
||||||
|
use app\admin\model\v1\SysImageModel;
|
||||||
|
use Intervention\Image\ImageManager;
|
||||||
|
use think\facade\Filesystem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传控制器
|
||||||
|
*/
|
||||||
|
class Upload
|
||||||
|
{
|
||||||
|
// 上传图片
|
||||||
|
public function image()
|
||||||
|
{
|
||||||
|
$param = request()->param(['module' => 'unknown']);
|
||||||
|
if (is_null($param)) {
|
||||||
|
return error('请确定请求参数正确');
|
||||||
|
}
|
||||||
|
$file = request()->file('image');
|
||||||
|
if (is_null($file)) {
|
||||||
|
return error('请确定上传对象或字段是否正确');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$validate = validate([
|
||||||
|
'module' => 'require|max:64',
|
||||||
|
'image' => 'fileSize:1048576|fileExt:jpg,jpeg,png,gif'
|
||||||
|
]);
|
||||||
|
if (!$validate->check(['module' => $param['module'], 'image' => $file])) {
|
||||||
|
return error($validate->getError());
|
||||||
|
}
|
||||||
|
|
||||||
|
$storage = config('filesystem.disks.image.url');
|
||||||
|
|
||||||
|
$filemd5 = $file->md5();
|
||||||
|
$filesha1 = $file->sha1();
|
||||||
|
|
||||||
|
$image_model = SysImageModel::md5($filemd5)->find();
|
||||||
|
if (is_null($image_model)) {
|
||||||
|
$filename = Filesystem::disk('image')->putFile($param['module'], $file);
|
||||||
|
// 生成缩略图
|
||||||
|
$image_manager = new ImageManager(new \Intervention\Image\Drivers\Gd\Driver());
|
||||||
|
$image = $image_manager->read('.' . $storage . '/' . $filename);
|
||||||
|
$image->scale(200, 200);
|
||||||
|
$idx = strrpos($filename, '.');
|
||||||
|
$thumb_filename = mb_substr($filename, 0, $idx) . '_thumb.' . mb_substr($filename, $idx + 1);
|
||||||
|
$image->save('.' . $storage . '/' . $thumb_filename);
|
||||||
|
|
||||||
|
// 保存图片
|
||||||
|
$image_model = new SysImageModel();
|
||||||
|
$image_model->language_id = request()->lang_id;
|
||||||
|
$image_model->module = $param['module'];
|
||||||
|
$image_model->image_path = $filename;
|
||||||
|
$image_model->image_thumb = $thumb_filename;
|
||||||
|
$image_model->image_size = $file->getSize();
|
||||||
|
$image_model->image_type = $file->getOriginalMime();
|
||||||
|
$image_model->image_md5 = $filemd5;
|
||||||
|
$image_model->image_sha1 = $filesha1;
|
||||||
|
if (!$image_model->save()) {
|
||||||
|
return error('上传失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success('操作成功', [
|
||||||
|
'url' => $storage . '/' . $image_model->image_path,
|
||||||
|
'thumb_url' => $storage . '/' . $image_model->image_thumb,
|
||||||
|
'filemd5' => $image_model->image_md5,
|
||||||
|
'filesha1' => $image_model->image_sha1
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
return error($th->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return error('上传失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,12 +3,13 @@ declare (strict_types = 1);
|
|||||||
|
|
||||||
namespace app\admin\model\v1;
|
namespace app\admin\model\v1;
|
||||||
|
|
||||||
use app\common\model\ImageBaseModel;
|
use app\common\model\SysImageBaseModel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 图片上传模型
|
||||||
* @mixin \think\Model
|
* @mixin \think\Model
|
||||||
*/
|
*/
|
||||||
class ImageModel extends ImageBaseModel
|
class SysImageModel extends SysImageBaseModel
|
||||||
{
|
{
|
||||||
// 根据md5获取图片
|
// 根据md5获取图片
|
||||||
public function scopeMd5($query, $md5)
|
public function scopeMd5($query, $md5)
|
||||||
@@ -38,7 +38,7 @@ Route::group('v1', function () {
|
|||||||
// 图片管理
|
// 图片管理
|
||||||
Route::group('images', function () {
|
Route::group('images', function () {
|
||||||
// 图片上传
|
// 图片上传
|
||||||
Route::post('/:module/upload', 'Images/upload');
|
Route::post('/:module/upload', 'Upload/image');
|
||||||
});
|
});
|
||||||
|
|
||||||
// 文章模块
|
// 文章模块
|
||||||
@@ -213,7 +213,6 @@ Route::group('v1', function () {
|
|||||||
// 产品回收站删除
|
// 产品回收站删除
|
||||||
Route::delete('delete/:id', 'ProductTrash/delete');
|
Route::delete('delete/:id', 'ProductTrash/delete');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
})->prefix('v1.');
|
})->prefix('v1.');
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use think\Model;
|
|||||||
/**
|
/**
|
||||||
* @mixin \think\Model
|
* @mixin \think\Model
|
||||||
*/
|
*/
|
||||||
class ImageBaseModel extends Model
|
class SysImageBaseModel extends Model
|
||||||
{
|
{
|
||||||
// 表名
|
// 表名
|
||||||
protected $name = 'sys_image';
|
protected $name = 'sys_image';
|
||||||
@@ -27,7 +27,7 @@ class CreateSysImage extends Migrator
|
|||||||
*/
|
*/
|
||||||
public function change()
|
public function change()
|
||||||
{
|
{
|
||||||
$table = $this->table('sys_image', ['engine' => 'MyISAM', 'comment' => '图片表']);
|
$table = $this->table('sys_image', ['engine' => 'MyISAM', 'comment' => '图片上传表']);
|
||||||
$table->addColumn('language_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '语言ID'])
|
$table->addColumn('language_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '语言ID'])
|
||||||
->addColumn('module', 'string', ['limit' => 64, 'null' => false, 'comment' => '图片所属模块'])
|
->addColumn('module', 'string', ['limit' => 64, 'null' => false, 'comment' => '图片所属模块'])
|
||||||
->addColumn('image_path', 'string', ['limit' => 125, 'null' => false, 'comment' => '图片路径'])
|
->addColumn('image_path', 'string', ['limit' => 125, 'null' => false, 'comment' => '图片路径'])
|
||||||
|
|||||||
Reference in New Issue
Block a user