mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-05 19:41:44 +08:00
修改文件上传
This commit is contained in:
parent
2c3c98457b
commit
193195c79f
@ -139,7 +139,7 @@ class Upload extends Controller
|
||||
if (in_array($extension, ['jpg', 'gif', 'png', 'bmp', 'jpeg', 'wbmp'])) {
|
||||
[$width, $height] = getimagesize($distname);
|
||||
if ($width < 1 || $height < 1 && $local->del($this->name)) {
|
||||
return json(['uploaded' => false, 'error' => ['message' => '图片尺寸读取失败!']]);
|
||||
return json(['uploaded' => false, 'error' => ['message' => '图片读取尺寸失败!']]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
6
vendor/composer/InstalledVersions.php
vendored
6
vendor/composer/InstalledVersions.php
vendored
@ -32,7 +32,7 @@ private static $installed = array (
|
||||
'aliases' =>
|
||||
array (
|
||||
),
|
||||
'reference' => 'dec53f9ca9b07c3d9ce31ddaf6553b7c98c22389',
|
||||
'reference' => '2c3c98457bfabfff63dacfc826d66f0847c04145',
|
||||
'name' => 'zoujingli/thinkadmin',
|
||||
),
|
||||
'versions' =>
|
||||
@ -171,7 +171,7 @@ private static $installed = array (
|
||||
array (
|
||||
0 => '9999999-dev',
|
||||
),
|
||||
'reference' => 'aaeb9dc81e7e93b6476460a9f58e752aa1e5f3ac',
|
||||
'reference' => '066e557c25c299f7df8c36f0ea602473a4229156',
|
||||
),
|
||||
'zoujingli/thinkadmin' =>
|
||||
array (
|
||||
@ -180,7 +180,7 @@ private static $installed = array (
|
||||
'aliases' =>
|
||||
array (
|
||||
),
|
||||
'reference' => 'dec53f9ca9b07c3d9ce31ddaf6553b7c98c22389',
|
||||
'reference' => '2c3c98457bfabfff63dacfc826d66f0847c04145',
|
||||
),
|
||||
'zoujingli/wechat-developer' =>
|
||||
array (
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -325,7 +325,6 @@ 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',
|
||||
|
1
vendor/composer/autoload_static.php
vendored
1
vendor/composer/autoload_static.php
vendored
@ -454,7 +454,6 @@ 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',
|
||||
|
8
vendor/composer/installed.json
vendored
8
vendor/composer/installed.json
vendored
@ -849,12 +849,12 @@
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/zoujingli/ThinkLibrary.git",
|
||||
"reference": "aaeb9dc81e7e93b6476460a9f58e752aa1e5f3ac"
|
||||
"reference": "066e557c25c299f7df8c36f0ea602473a4229156"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/aaeb9dc81e7e93b6476460a9f58e752aa1e5f3ac",
|
||||
"reference": "aaeb9dc81e7e93b6476460a9f58e752aa1e5f3ac",
|
||||
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/066e557c25c299f7df8c36f0ea602473a4229156",
|
||||
"reference": "066e557c25c299f7df8c36f0ea602473a4229156",
|
||||
"shasum": "",
|
||||
"mirrors": [
|
||||
{
|
||||
@ -871,7 +871,7 @@
|
||||
"ext-mbstring": "*",
|
||||
"topthink/framework": "^6.0"
|
||||
},
|
||||
"time": "2021-05-13T08:42:48+00:00",
|
||||
"time": "2021-05-13T08:49:03+00:00",
|
||||
"default-branch": true,
|
||||
"type": "library",
|
||||
"extra": {
|
||||
|
6
vendor/composer/installed.php
vendored
6
vendor/composer/installed.php
vendored
@ -6,7 +6,7 @@
|
||||
'aliases' =>
|
||||
array (
|
||||
),
|
||||
'reference' => 'dec53f9ca9b07c3d9ce31ddaf6553b7c98c22389',
|
||||
'reference' => '2c3c98457bfabfff63dacfc826d66f0847c04145',
|
||||
'name' => 'zoujingli/thinkadmin',
|
||||
),
|
||||
'versions' =>
|
||||
@ -145,7 +145,7 @@
|
||||
array (
|
||||
0 => '9999999-dev',
|
||||
),
|
||||
'reference' => 'aaeb9dc81e7e93b6476460a9f58e752aa1e5f3ac',
|
||||
'reference' => '066e557c25c299f7df8c36f0ea602473a4229156',
|
||||
),
|
||||
'zoujingli/thinkadmin' =>
|
||||
array (
|
||||
@ -154,7 +154,7 @@
|
||||
'aliases' =>
|
||||
array (
|
||||
),
|
||||
'reference' => 'dec53f9ca9b07c3d9ce31ddaf6553b7c98c22389',
|
||||
'reference' => '2c3c98457bfabfff63dacfc826d66f0847c04145',
|
||||
),
|
||||
'zoujingli/wechat-developer' =>
|
||||
array (
|
||||
|
2
vendor/services.php
vendored
2
vendor/services.php
vendored
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
// This file is automatically generated at:2021-05-13 16:43:36
|
||||
// This file is automatically generated at:2021-05-13 16:50:37
|
||||
declare (strict_types = 1);
|
||||
return array (
|
||||
0 => 'think\\admin\\Library',
|
||||
|
@ -1,149 +0,0 @@
|
||||
<?php
|
||||
|
||||
// +----------------------------------------------------------------------
|
||||
// | Library for ThinkAdmin
|
||||
// +----------------------------------------------------------------------
|
||||
// | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
|
||||
// +----------------------------------------------------------------------
|
||||
// | 开源协议 ( https://mit-license.org )
|
||||
// +----------------------------------------------------------------------
|
||||
// | gitee 仓库地址 :https://gitee.com/zoujingli/ThinkLibrary
|
||||
// | github 仓库地址 :https://github.com/zoujingli/ThinkLibrary
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace think\admin\extend;
|
||||
|
||||
/**
|
||||
* Image 压缩工具
|
||||
* Class ImageExtend
|
||||
* @package think\admin\extend
|
||||
*/
|
||||
class ImageExtend
|
||||
{
|
||||
private $src;
|
||||
private $image;
|
||||
private $imageinfo;
|
||||
private $percent = 0.5;
|
||||
|
||||
/**
|
||||
* 图片压缩
|
||||
* @param string $src 源图
|
||||
* @param float $percent 压缩比例
|
||||
*/
|
||||
public function __construct(string $src, float $percent = 1)
|
||||
{
|
||||
$this->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] = getimagesize($this->src);
|
||||
if ($width < 1 || $height < 1) return [0, '读取图片尺寸失败!'];
|
||||
$this->imageinfo = ['width' => $width, 'height' => $height, 'type' => image_type_to_extension($type, false)];
|
||||
$fun = "imagecreatefrom{$this->imageinfo['type']}";
|
||||
imagealphablending($this->image = $fun($this->src), true);
|
||||
return $this->_thumpImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部:操作图片
|
||||
*/
|
||||
private function _thumpImage(): array
|
||||
{
|
||||
[$srcWidth, $srcHeight] = [$this->imageinfo['width'], $this->imageinfo['height']];
|
||||
[$newWidth, $newHeight] = [intval($srcWidth * $this->percent), intval($srcHeight * $this->percent)];
|
||||
[$srcThumps, $dstBackup] = [imagecreatetruecolor($newWidth, $newHeight), imagecreatetruecolor($srcWidth, $srcHeight)];
|
||||
|
||||
[imagealphablending($srcThumps, false), imagesavealpha($srcThumps, true)];
|
||||
[imagealphablending($dstBackup, false), imagesavealpha($dstBackup, true)];
|
||||
|
||||
imagecopyresampled($srcThumps, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $srcWidth, $srcHeight);
|
||||
imagecopyresampled($dstBackup, $srcThumps, 0, 0, 0, 0, $srcWidth, $srcHeight, $newWidth, $newHeight);
|
||||
|
||||
[imagedestroy($srcThumps), imagedestroy($this->image)];
|
||||
|
||||
$this->image = $dstBackup;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user