diff --git a/app/admin/controller/api/Upload.php b/app/admin/controller/api/Upload.php index 1be8c1d46..f8a074878 100644 --- a/app/admin/controller/api/Upload.php +++ b/app/admin/controller/api/Upload.php @@ -17,11 +17,13 @@ namespace app\admin\controller\api; use think\admin\Controller; +use think\admin\extend\ImageExtend; use think\admin\Storage; use think\admin\storage\AliossStorage; use think\admin\storage\LocalStorage; use think\admin\storage\QiniuStorage; use think\admin\storage\TxcosStorage; +use think\exception\HttpResponseException; use think\file\UploadedFile; use think\Response; use think\response\Json; @@ -120,6 +122,7 @@ class Upload extends Controller return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件过大或未上传!']]); } $extension = strtolower($file->getOriginalExtension()); + [$pathname, $original] = [$file->getPathname(), $file->getOriginalName()]; if (!in_array($extension, str2arr(sysconf('storage.allow_exts')))) { return json(['uploaded' => false, 'error' => ['message' => '文件类型受限,请在后台配置规则!']]); } @@ -127,21 +130,25 @@ class Upload extends Controller return json(['uploaded' => false, 'error' => ['message' => '文件安全保护,可执行文件禁止上传!']]); } [$this->type, $this->safe] = [$this->getType(), $this->getSafe()]; - $this->name = input('key') ?: Storage::name($file->getPathname(), $extension, '', 'md5_file'); + $this->name = input('key') ?: Storage::name($pathname, $extension, '', 'md5_file'); try { if ($this->type === 'local') { $local = LocalStorage::instance(); - $realpath = dirname($realname = $local->path($this->name, $this->safe)); - file_exists($realpath) && is_dir($realpath) || mkdir($realpath, 0755, true); - if (rename($file->getPathname(), $realname)) { - $info = $local->info($this->name, $this->safe, $file->getOriginalName()); - } else { - return json(['uploaded' => false, 'error' => ['message' => '文件移动处理失败!']]); + $distname = $local->path($this->name, $this->safe); + $file->move(dirname($distname), basename($distname)); + $info = $local->info($this->name, $this->safe, $original); + if (in_array($extension, ['jpg', 'gif', 'png', 'bmp', 'jpeg', 'wbmp'])) { + [$status, $message] = (new ImageExtend($distname))->compress($distname); + if (empty($status) && $local->del($this->name)) { + return json(['uploaded' => false, 'error' => ['message' => $message]]); + } } } else { - $bina = file_get_contents($file->getRealPath()); - $info = Storage::instance($this->type)->set($this->name, $bina, $this->safe, $file->getOriginalName()); + $bina = file_get_contents($pathname); + $info = Storage::instance($this->type)->set($this->name, $bina, $this->safe, $original); } + } catch (HttpResponseException $exception) { + throw $exception; } catch (\Exception $exception) { return json(['uploaded' => false, 'error' => ['message' => $exception->getMessage()]]); } @@ -188,8 +195,10 @@ class Upload extends Controller if ($file instanceof UploadedFile) { return $file; } else { - $this->error('读取文件对象失败!'); + $this->error('未获取到上传的文件对象!'); } + } catch (HttpResponseException $exception) { + throw $exception; } catch (\Exception $exception) { $this->error(lang($exception->getMessage())); } diff --git a/vendor/composer/InstalledVersions.php b/vendor/composer/InstalledVersions.php index 7cee43b33..186f351a5 100644 --- a/vendor/composer/InstalledVersions.php +++ b/vendor/composer/InstalledVersions.php @@ -32,7 +32,7 @@ private static $installed = array ( 'aliases' => array ( ), - 'reference' => 'd9f0f0bdb0a35ab7eb7e56f35a470ee069efb900', + 'reference' => 'f00305015a80eabe31474a75c9147214cb9d8585', 'name' => 'zoujingli/thinkadmin', ), 'versions' => @@ -171,7 +171,7 @@ private static $installed = array ( array ( 0 => '9999999-dev', ), - 'reference' => 'aef0fcd19d5ad9c49ed2da1001243a5d5d9c9ca2', + 'reference' => '202f84ff3aabdc4cde3f8d1f0933a05cdd4d7172', ), 'zoujingli/thinkadmin' => array ( @@ -180,7 +180,7 @@ private static $installed = array ( 'aliases' => array ( ), - 'reference' => 'd9f0f0bdb0a35ab7eb7e56f35a470ee069efb900', + 'reference' => 'f00305015a80eabe31474a75c9147214cb9d8585', ), 'zoujingli/wechat-developer' => array ( diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 8a6761f30..7d3d152c6 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -325,6 +325,7 @@ return array( 'think\\admin\\extend\\DataExtend' => $vendorDir . '/zoujingli/think-library/src/extend/DataExtend.php', 'think\\admin\\extend\\ExcelExtend' => $vendorDir . '/zoujingli/think-library/src/extend/ExcelExtend.php', 'think\\admin\\extend\\HttpExtend' => $vendorDir . '/zoujingli/think-library/src/extend/HttpExtend.php', + 'think\\admin\\extend\\ImageExtend' => $vendorDir . '/zoujingli/think-library/src/extend/ImageExtend.php', 'think\\admin\\extend\\JsonRpcClient' => $vendorDir . '/zoujingli/think-library/src/extend/JsonRpcClient.php', 'think\\admin\\extend\\JsonRpcServer' => $vendorDir . '/zoujingli/think-library/src/extend/JsonRpcServer.php', 'think\\admin\\extend\\Parsedown' => $vendorDir . '/zoujingli/think-library/src/extend/Parsedown.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index c2e397792..ed1b77895 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -454,6 +454,7 @@ class ComposerStaticInit3e3e984682c06e656fe76c5d84347fb3 'think\\admin\\extend\\DataExtend' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/DataExtend.php', 'think\\admin\\extend\\ExcelExtend' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/ExcelExtend.php', 'think\\admin\\extend\\HttpExtend' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/HttpExtend.php', + 'think\\admin\\extend\\ImageExtend' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/ImageExtend.php', 'think\\admin\\extend\\JsonRpcClient' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/JsonRpcClient.php', 'think\\admin\\extend\\JsonRpcServer' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/JsonRpcServer.php', 'think\\admin\\extend\\Parsedown' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/Parsedown.php', diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 6e6bf8a48..77b37dfb0 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -849,12 +849,12 @@ "source": { "type": "git", "url": "https://github.com/zoujingli/ThinkLibrary.git", - "reference": "aef0fcd19d5ad9c49ed2da1001243a5d5d9c9ca2" + "reference": "202f84ff3aabdc4cde3f8d1f0933a05cdd4d7172" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/aef0fcd19d5ad9c49ed2da1001243a5d5d9c9ca2", - "reference": "aef0fcd19d5ad9c49ed2da1001243a5d5d9c9ca2", + "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/202f84ff3aabdc4cde3f8d1f0933a05cdd4d7172", + "reference": "202f84ff3aabdc4cde3f8d1f0933a05cdd4d7172", "shasum": "", "mirrors": [ { @@ -871,7 +871,7 @@ "ext-mbstring": "*", "topthink/framework": "^6.0" }, - "time": "2021-05-06T02:52:35+00:00", + "time": "2021-05-12T08:50:33+00:00", "default-branch": true, "type": "library", "extra": { diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index b7400783c..bfc6c376b 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -6,7 +6,7 @@ 'aliases' => array ( ), - 'reference' => 'd9f0f0bdb0a35ab7eb7e56f35a470ee069efb900', + 'reference' => 'f00305015a80eabe31474a75c9147214cb9d8585', 'name' => 'zoujingli/thinkadmin', ), 'versions' => @@ -145,7 +145,7 @@ array ( 0 => '9999999-dev', ), - 'reference' => 'aef0fcd19d5ad9c49ed2da1001243a5d5d9c9ca2', + 'reference' => '202f84ff3aabdc4cde3f8d1f0933a05cdd4d7172', ), 'zoujingli/thinkadmin' => array ( @@ -154,7 +154,7 @@ 'aliases' => array ( ), - 'reference' => 'd9f0f0bdb0a35ab7eb7e56f35a470ee069efb900', + 'reference' => 'f00305015a80eabe31474a75c9147214cb9d8585', ), 'zoujingli/wechat-developer' => array ( diff --git a/vendor/services.php b/vendor/services.php index 3fec6fc7a..d1a74766d 100644 --- a/vendor/services.php +++ b/vendor/services.php @@ -1,5 +1,5 @@ 'think\\admin\\Library', diff --git a/vendor/zoujingli/think-library/src/extend/ImageExtend.php b/vendor/zoujingli/think-library/src/extend/ImageExtend.php new file mode 100644 index 000000000..6164e37ac --- /dev/null +++ b/vendor/zoujingli/think-library/src/extend/ImageExtend.php @@ -0,0 +1,143 @@ +src = $src; + $this->percent = $percent; + } + + /** + * 高清压缩图片 + * @param string $saveName 提供图片名 + * @return array + */ + public function compress(string $saveName = ''): array + { + [$status, $message] = $this->_openImage(); + if (empty($status)) { + return [0, $message]; + } elseif (empty($saveName)) { + return $this->_showImage(); + } else { + return $this->_saveImage($saveName); + } + } + + /** + * 内部:打开图片 + * @return array + */ + private function _openImage(): array + { + [$width, $height, $type, $attr] = getimagesize($this->src); + if ($width < 1 || $height < 1) return [0, '读取图片尺寸失败!']; + $this->imageinfo = ['width' => $width, 'height' => $height, 'attr' => $attr, 'type' => image_type_to_extension($type, false)]; + $fun = "imagecreatefrom{$this->imageinfo['type']}"; + $this->image = $fun($this->src); + return $this->_thumpImage(); + } + + /** + * 内部:操作图片 + */ + private function _thumpImage(): array + { + $newWidth = intval($this->imageinfo['width'] * $this->percent); + $newHeight = intval($this->imageinfo['height'] * $this->percent); + $imgThumps = imagecreatetruecolor($newWidth, $newHeight); + // 将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度 + imagecopyresampled($imgThumps, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $this->imageinfo['width'], $this->imageinfo['height']); + imagedestroy($this->image); + $this->image = $imgThumps; + return [1, '图片压缩成功']; + } + + /** + * 输出图片:保存图片则用 saveImage() + * @return array + */ + private function _showImage(): array + { + header("Content-Type: image/{$this->imageinfo['type']}"); + $funcs = "image{$this->imageinfo['type']}"; + $funcs($this->image); + return [1, '图片内容输出成功']; + } + + /** + * 保存图片到硬盘: + * @param string $dstImgName + * @return array + */ + private function _saveImage(string $dstImgName): array + { + if (empty($dstImgName)) return [0, '未指定存储目标路径']; + + // 如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名 + $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp', '.gif']; + [$srcExt, $dstExt] = [strrchr($this->src, "."), strrchr($dstImgName, ".")]; + if (!empty($srcExt)) $srcExt = strtolower($srcExt); + if (!empty($dstExt)) $dstExt = strtolower($dstExt); + + // 有指定目标名扩展名 + if (!empty($dstExt) && in_array($dstExt, $allowImgs)) { + $dstName = $dstImgName; + } elseif (!empty($srcExt) && in_array($srcExt, $allowImgs)) { + $dstName = $dstImgName . $srcExt; + } else { + $dstName = $dstImgName . $this->imageinfo['type']; + } + + // 图片内容转换存储 + $image = "image{$this->imageinfo['type']}"; + if ($image($this->image, $dstName)) { + return [1, '图片转换存储成功!']; + } else { + return [0, '图片转换存储失败!']; + } + } + + /** + * 销毁图片 + */ + public function __destruct() + { + if (is_resource($this->image)) { + imagedestroy($this->image); + } + } +} \ No newline at end of file