diff --git a/.gitignore b/.gitignore index 5054a67e..14833e71 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ composer.phar composer.lock .DS_Store Thumbs.db + /.idea /.vscode /vendor diff --git a/app/admin/controller/v1/Images.php b/app/admin/controller/v1/Images.php new file mode 100644 index 00000000..8a0dedc7 --- /dev/null +++ b/app/admin/controller/v1/Images.php @@ -0,0 +1,65 @@ +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('上传失败'); + } +} diff --git a/app/admin/model/v1/ImageModel.php b/app/admin/model/v1/ImageModel.php new file mode 100644 index 00000000..7a87b7b2 --- /dev/null +++ b/app/admin/model/v1/ImageModel.php @@ -0,0 +1,18 @@ +where('image_md5', '=', $md5); + } +} diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index ae5ed125..1d7dae15 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -26,6 +26,12 @@ Route::group('v1', function () { Route::post('login', 'Login/index'); }); + // 图片管理 + Route::group('images', function () { + // 图片上传 + Route::post('upload', 'Images/upload'); + }); + // 文章模块 Route::group('article', function () { // 文章列表 diff --git a/app/common/model/ImageBaseModel.php b/app/common/model/ImageBaseModel.php new file mode 100644 index 00000000..b843d412 --- /dev/null +++ b/app/common/model/ImageBaseModel.php @@ -0,0 +1,33 @@ + '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', + ]; +}