feat: 新增图片上传接口
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,6 +4,7 @@ composer.phar
|
|||||||
composer.lock
|
composer.lock
|
||||||
.DS_Store
|
.DS_Store
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
|
|
||||||
/.idea
|
/.idea
|
||||||
/.vscode
|
/.vscode
|
||||||
/vendor
|
/vendor
|
||||||
|
|||||||
65
app/admin/controller/v1/Images.php
Normal file
65
app/admin/controller/v1/Images.php
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\admin\controller\v1;
|
||||||
|
|
||||||
|
use app\admin\model\v1\ImageModel;
|
||||||
|
use Intervention\Image\ImageManager;
|
||||||
|
use think\facade\Filesystem;
|
||||||
|
|
||||||
|
class Images
|
||||||
|
{
|
||||||
|
// 上传
|
||||||
|
public function upload()
|
||||||
|
{
|
||||||
|
$file = request()->file('image');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$validate = validate([
|
||||||
|
'image'=>'fileSize:1048576|fileExt:jpg,jpeg,png,gif'
|
||||||
|
]);
|
||||||
|
if (!$validate->check(['image' => $file])) {
|
||||||
|
return error($validate->getError());
|
||||||
|
}
|
||||||
|
|
||||||
|
$filemd5 = $file->md5();
|
||||||
|
$filesha1 = $file->sha1();
|
||||||
|
$filename = Filesystem::disk('public')->putFile('images', $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 = ImageModel::md5($filemd5)->find();
|
||||||
|
if (is_null($image_model)) {
|
||||||
|
$image_model = new ImageModel();
|
||||||
|
}
|
||||||
|
$image_model->language_id = request()->lang_id;
|
||||||
|
$image_model->image_name = $file->getOriginalName();
|
||||||
|
$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/' . $filename,
|
||||||
|
'thumb_url' => '/storage/' . $thumb_filename,
|
||||||
|
'filemd5' => $filemd5,
|
||||||
|
'filesha1' => $filesha1
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
return error($th->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return error('上传失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
18
app/admin/model/v1/ImageModel.php
Normal file
18
app/admin/model/v1/ImageModel.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\admin\model\v1;
|
||||||
|
|
||||||
|
use app\common\model\ImageBaseModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ImageModel extends ImageBaseModel
|
||||||
|
{
|
||||||
|
// 根据md5获取图片
|
||||||
|
public function scopeMd5($query, $md5)
|
||||||
|
{
|
||||||
|
$query->where('image_md5', '=', $md5);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,12 @@ Route::group('v1', function () {
|
|||||||
Route::post('login', 'Login/index');
|
Route::post('login', 'Login/index');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 图片管理
|
||||||
|
Route::group('images', function () {
|
||||||
|
// 图片上传
|
||||||
|
Route::post('upload', 'Images/upload');
|
||||||
|
});
|
||||||
|
|
||||||
// 文章模块
|
// 文章模块
|
||||||
Route::group('article', function () {
|
Route::group('article', function () {
|
||||||
// 文章列表
|
// 文章列表
|
||||||
|
|||||||
33
app/common/model/ImageBaseModel.php
Normal file
33
app/common/model/ImageBaseModel.php
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\common\model;
|
||||||
|
|
||||||
|
use think\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class ImageBaseModel extends Model
|
||||||
|
{
|
||||||
|
// 表名
|
||||||
|
protected $name = 'sys_image';
|
||||||
|
|
||||||
|
// 主键
|
||||||
|
protected $pk = 'id';
|
||||||
|
|
||||||
|
// 字段信息
|
||||||
|
protected $schema = [
|
||||||
|
'id' => 'int',
|
||||||
|
'language_id' => 'int',
|
||||||
|
'image_name' => 'string',
|
||||||
|
'image_path' => 'string',
|
||||||
|
'image_thumb' => 'string',
|
||||||
|
'image_size' => 'int',
|
||||||
|
'image_type' => 'string',
|
||||||
|
'image_md5' => 'string',
|
||||||
|
'image_sha1' => 'string',
|
||||||
|
'created_at' => 'datetime',
|
||||||
|
'deleted_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user