diff --git a/.env.example b/.env.example index a558ef087..feef3f0e5 100644 --- a/.env.example +++ b/.env.example @@ -15,3 +15,4 @@ prefix = lsky_ single_user_mode = false intercept_salacity = false url_query = '' +watermark = false \ No newline at end of file diff --git a/application/common/controller/Upload.php b/application/common/controller/Upload.php index 74f10c6ac..f1304be20 100644 --- a/application/common/controller/Upload.php +++ b/application/common/controller/Upload.php @@ -15,6 +15,7 @@ use think\Controller; use think\Exception; use think\facade\Config; +use think\Image; class Upload extends Controller { @@ -90,6 +91,8 @@ public function exec() } } + $temp = $image->getPathname(); + // 当前储存策略 $currentStrategy = strtolower($this->group->strategy); $pathname = $this->makePathname($image->getInfo('name')); @@ -103,6 +106,45 @@ public function exec() } $url = make_url($domain, $pathname); + // 自动水印 + if (Config::get('system.watermark') && $watermarkConfig = config("watermark.{$currentStrategy}")) { + if ($watermarkConfig['enable']) { + $watermarkImage = app()->getRuntimePath() . 'temp/' . md5($sha1.$md5); + $locates = [ + 1 => Image::WATER_NORTHWEST, 2 => Image::WATER_NORTH, 3 => Image::WATER_NORTHEAST, + 4 => Image::WATER_WEST, 5 => Image::WATER_CENTER, 6 => Image::WATER_EAST, + 7 => Image::WATER_SOUTHWEST, 8 => Image::WATER_SOUTH, 9 => Image::WATER_SOUTHEAST, + ]; + switch ($watermarkConfig['type']) { + case 1: + $watermark = Image::open($image)->text( + $watermarkConfig['text'], + $watermarkConfig['font'], + $watermarkConfig['size'], + $watermarkConfig['color'], + $locates[$watermarkConfig['locate']], + $watermarkConfig['offset'], + $watermarkConfig['angle'] + ); + break; + case 2: + $watermark = Image::open($image)->water( + $watermarkConfig['source'], + $watermarkConfig['locate'], + $watermarkConfig['alpha'] + ); + break; + default: + throw new Exception('自动水印功能配置异常'); + } + $watermark->save($watermarkImage); + $temp = $watermarkImage; + $sha1 = sha1_file($temp); + $md5 = md5_file($temp); + $size = filesize($temp); + } + } + // 检测是否存在该图片,有则直接返回 if ($oldImage = Images::where('md5', $md5)->where('sha1', $sha1)->where('strategy', $currentStrategy)->find()) { $pathname = $oldImage->pathname; @@ -110,13 +152,15 @@ public function exec() goto exist; } - if (!$this->strategy->create($pathname, $image->getPathname())) { + if (!$this->strategy->create($pathname, $temp)) { if (Config::get('app.app_debug')) { throw new Exception($this->strategy->getError()); } throw new Exception('上传失败,请检查策略配置是否正确!'); } + isset($watermarkImage) && @unlink($watermarkImage); + exist: // 图片鉴黄 diff --git a/config/system.php b/config/system.php index 66437774e..14a9e12ce 100644 --- a/config/system.php +++ b/config/system.php @@ -12,5 +12,7 @@ // 违规图片是否直接拦截 'intercept_salacity' => env('system.intercept_salacity', false), // 图片链接额外参数(例: ?test=123) - 'url_query' => env('system.url_query', '') + 'url_query' => env('system.url_query', ''), + // 是否开启策略自动水印功能 + 'watermark' => env('system.watermark') ]; diff --git a/config/watermark.php b/config/watermark.php new file mode 100644 index 000000000..951b0d2c9 --- /dev/null +++ b/config/watermark.php @@ -0,0 +1,70 @@ +getRootPath(); + +return [ + // 本地 + 'local' => [ + 'enable' => false, + 'type' => 1, + 'text' => 'Lsky Pro', + 'font' => $root . 'public/static/app/font/test.ttf', + 'size' => 20, + 'color' => '#333333', + 'locate' => 9, + 'offset' => 0, + 'angle' => 0, + ], + // 阿里云 OSS + 'oss' => [ + 'enable' => false, + 'type' => 2, + 'source' => $root . 'public/static/app/images/icon.png', + 'locate' => 9, + 'alpha' => 100, + ], + // 腾讯云 COS + 'cos' => [ + 'enable' => false, + ], + // 又拍云 USS + 'uss' => [ + 'enable' => false, + ], + // 七牛云 Kodo + 'kodo' => [ + 'enable' => false, + ], +]; \ No newline at end of file diff --git a/extend/strategy/driver/Local.php b/extend/strategy/driver/Local.php index a6906f548..3c57679a5 100644 --- a/extend/strategy/driver/Local.php +++ b/extend/strategy/driver/Local.php @@ -61,7 +61,7 @@ public function create($pathname, $file) { $path = $this->rootPath . dirname($pathname) . DIRECTORY_SEPARATOR; if (true === $this->checkPath($path)) { - if (move_uploaded_file($file, $pathname)) { + if (rename($file, $pathname)) { return true; } } diff --git a/public/static/app/font/test.ttf b/public/static/app/font/test.ttf new file mode 100644 index 000000000..a5638aa00 Binary files /dev/null and b/public/static/app/font/test.ttf differ