123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- <?php
- namespace upload;
- class Uploader
- {
- /**
- * @title: 所有可上传文件的mime数组
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-05-18 14:23:37
- */
- public static $mimes = [
- 'xl' => 'application/excel',
- 'js' => 'application/javascript',
- 'hqx' => 'application/mac-binhex40',
- 'cpt' => 'application/mac-compactpro',
- 'bin' => 'application/macbinary',
- 'doc' => 'application/msword',
- 'word' => 'application/msword',
- 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
- 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
- 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
- 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
- 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
- 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- // 'docx' => 'application/zip',
- 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
- 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
- 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
- 'class' => 'application/octet-stream',
- 'dll' => 'application/octet-stream',
- 'dms' => 'application/octet-stream',
- 'exe' => 'application/octet-stream',
- 'lha' => 'application/octet-stream',
- 'lzh' => 'application/octet-stream',
- 'psd' => 'application/octet-stream',
- 'sea' => 'application/octet-stream',
- 'so' => 'application/octet-stream',
- 'oda' => 'application/oda',
- 'pdf' => 'application/pdf',
- 'ai' => 'application/postscript',
- 'eps' => 'application/postscript',
- 'ps' => 'application/postscript',
- 'smi' => 'application/smil',
- 'smil' => 'application/smil',
- 'mif' => 'application/vnd.mif',
- 'xls' => 'application/vnd.ms-excel',
- 'ppt' => 'application/vnd.ms-powerpoint',
- 'wbxml' => 'application/vnd.wap.wbxml',
- 'wmlc' => 'application/vnd.wap.wmlc',
- 'dcr' => 'application/x-director',
- 'dir' => 'application/x-director',
- 'dxr' => 'application/x-director',
- 'dvi' => 'application/x-dvi',
- 'gtar' => 'application/x-gtar',
- 'php3' => 'application/x-httpd-php',
- 'php4' => 'application/x-httpd-php',
- 'php' => 'application/x-httpd-php',
- 'phtml' => 'application/x-httpd-php',
- 'phps' => 'application/x-httpd-php-source',
- 'swf' => 'application/x-shockwave-flash',
- 'sit' => 'application/x-stuffit',
- 'tar' => 'application/x-tar',
- 'tgz' => 'application/x-tar',
- 'json'=> 'application/json',
- 'xht' => 'application/xhtml+xml',
- 'xhtml' => 'application/xhtml+xml',
- 'zip' => 'application/zip',
- 'mid' => 'audio/midi',
- 'midi' => 'audio/midi',
- 'mp2' => 'audio/mpeg',
- 'mp3' => 'audio/mpeg',
- 'm4a' => 'audio/mp4',
- 'mpga' => 'audio/mpeg',
- 'aif' => 'audio/x-aiff',
- 'aifc' => 'audio/x-aiff',
- 'aiff' => 'audio/x-aiff',
- 'ram' => 'audio/x-pn-realaudio',
- 'rm' => 'audio/x-pn-realaudio',
- 'rpm' => 'audio/x-pn-realaudio-plugin',
- 'ra' => 'audio/x-realaudio',
- 'wav' => 'audio/x-wav',
- 'mka' => 'audio/x-matroska',
- 'bmp' => 'image/bmp',
- 'gif' => 'image/gif',
- 'jpeg' => 'image/jpeg',
- 'jpe' => 'image/jpeg',
- 'jpg' => 'image/jpeg',
- 'png' => 'image/png',
- 'tiff' => 'image/tiff',
- 'tif' => 'image/tiff',
- 'webp' => 'image/webp',
- 'heif' => 'image/heif',
- 'heifs' => 'image/heif-sequence',
- 'heic' => 'image/heic',
- 'heics' => 'image/heic-sequence',
- 'eml' => 'message/rfc822',
- 'css' => 'text/css',
- 'html' => 'text/html',
- 'htm' => 'text/html',
- 'shtml' => 'text/html',
- 'log' => 'text/plain',
- 'text' => 'text/plain',
- 'txt' => 'text/plain',
- 'rtx' => 'text/richtext',
- 'rtf' => 'text/rtf',
- 'vcf' => 'text/vcard',
- 'vcard' => 'text/vcard',
- 'ics' => 'text/calendar',
- 'xml' => 'text/xml',
- 'xsl' => 'text/xml',
- 'wmv' => 'video/x-ms-wmv',
- 'mpeg' => 'video/mpeg',
- 'mpe' => 'video/mpeg',
- 'mpg' => 'video/mpeg',
- 'mp4' => 'video/mp4',
- 'm4v' => 'video/mp4',
- 'mov' => 'video/quicktime',
- 'qt' => 'video/quicktime',
- 'rv' => 'video/vnd.rn-realvideo',
- 'avi' => 'video/x-msvideo',
- 'movie' => 'video/x-sgi-movie',
- 'webm' => 'video/webm',
- 'mkv' => 'video/x-matroska',
- 'apk' => 'application/vnd.android.package-archive',
- ];
- /**
- * @title: 文件上传配置
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-05-18 14:24:06
- */
- private $config = array(
- 'maxSize' => -1,// 上传文件的最大值
- 'supportMulti' => true,// 是否支持多文件上传
- 'allowExts' => array(),// 允许上传的文件后缀留空不作后缀检查
- 'allowTypes' => array(),// 允许上传的文件类型 留空不做检查
- 'autoSub' => false,// 启用子目录保存文件
- 'subType' => 'hash',// 子目录创建方式 可以使用hash date custom
- 'subDir' => '', //子目录名称 subType为custom方式后有效
- 'dateFormat' => 'Ymd',
- 'hashLevel' => 1, // hash的目录层次
- 'basePath' => '',// 上传文件保存路径
- 'autoCheck' => true, // 是否自动检查附件
- 'uploadReplace' => false,// 存在同名是否覆盖
- 'saveRule' => 'uniqid',// 上传文件命名规则,可选uniqid、md5、sha1
- 'hashType' => 'md5_file',// 上传文件Hash规则函数名
- 'Compress' => 1,//是否开启图片压缩
- 'attach_format_image' => 'jpg,jpeg,png,gif,bmp',
- 'attach_format_image_size' => '2MB',
- 'attach_format_video' => 'mp4,3gp,avi,mpeg',
- 'attach_format_video_size' => '50MB',
- 'attach_format_audio' => 'mp3,wav',
- 'attach_format_audio_size' => '5MB',
- 'attach_format_file' => 'xls,xlsx,txt,zip,apk',
- 'attach_format_file_size' => '2MB'
- );
- // 错误信息
- private $error = '';
- // 上传成功的文件信息
- private $uploadFileInfo ;
- // 获取配置
- public function __get($name){
- if(isset($this->config[$name])) {
- return $this->config[$name];
- }
- return null;
- }
- // 设置配置
- public function __set($name,$value){
- if(isset($this->config[$name])) {
- $this->config[$name] = $value;
- }
- }
- // 判断配置是否已设置
- public function __isset($name){
- return isset($this->config[$name]);
- }
- /**
- * @title: 架构函数
- * @desc: 当需要使用自定义的配置时,传$config参数即可
- * @param {array} {config} {} {自定义配置数组}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-05-18 14:25:18
- */
- public function __construct(array $config=[])
- {
- $this->config = array_merge($this->config,$config);
- // $this->saveRule = $this->config['saveRule'](time().GetRandStr(10));
- $this->basePath = empty($this->config['basePath'])?$this->basePath = public_path().'uploads'.DS:$this->basePath = $this->config['basePath'];
- }
- /**
- * 保存上传的单个文件
- * @access public
- * @param mixed $name 数据
- * @param string $value 数据表名
- * @return string
- */
- private function save($file) {
- $filename = $file['savepath'].$file['savename'];
- if(!$this->uploadReplace && is_file($filename)) {
- // 不覆盖同名文件
- $this->error = '文件已经存在!'.$filename;
- return false;
- }
- // 如果是图像文件 检测文件格式
- if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf'))) {
- $info = getimagesize($file['tmp_name']);
- if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){
- $this->error = '非法图像文件';
- return false;
- }
- }
- if(!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) {
- $this->error = '文件上传保存错误!';
- return false;
- }
- return true;
- }
- /**
- * @title: 检查自定义目录是否可以保存文件
- * @desc: 描述
- * @param {string} {savePath} {} {文件保存位置的绝对路径}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-05-18 15:01:45
- */
- private function checkCustomerDir($savePath = '')
- {
- if(!is_dir($savePath)) {
- // 检查目录是否编码后的
- if(is_dir(base64_decode($savePath))) {
- return base64_decode($savePath);
- }else{
- $this->error = '自定义上传目录'.$savePath.'不存在';
- return false;
- }
- }elseif(!is_writeable($savePath)) {
- $this->error = '自定义上传目录'.$savePath.'不可写';
- return false;
- }else{
- return $savePath;
- }
- }
- /**
- * @title: 开始上传文件
- * @desc: 描述
- * @param {string} {savePath} {} {文件保存路径,不传则使用默认位置}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-05-18 15:32:42
- */
- public function upload(string $savePath ='') {
- //检查用户自定义目录
- if(!empty($savePath)){
- // 当自定义路径无法保存时,返回false,并提示错误
- $savePath = $this->checkCustomerDir($savePath);
- if(!$savePath){
- return false;
- }
- $customerPath = true;
- }else{
- $customerPath = false;
- }
- $fileInfo = array();//用于保存上传的文件信息
- $isUpload = false;//判断是否已上传
- // 对$_FILES数组信息处理
- $files = $this->dealFiles();
- $finfo = finfo_open(FILEINFO_MIME);
- foreach($files as $key => $file) {
- //过滤无效的上传
- if(!empty($file['name'])) {
- //登记上传文件的扩展信息
- if(!isset($file['key'])) $file['key'] = $key;
- if(empty($file['tmp_name'])){
- return false;
- }
- $mimeInfo = finfo_file($finfo,$file['tmp_name']);
- $mimeInfo = $mimeInfo?explode(';',$mimeInfo)[0]:'';
- // xlsx和docx文件,通过mime识别可能会被识别为zip
- if($file['type'] == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' && $mimeInfo == 'application/zip'){
- $file['mime'] = $file['type'];
- }elseif($file['type']=='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' && $mimeInfo == 'application/zip'){
- $file['mime'] = $file['type'];
- }else{
- $file['mime'] = $mimeInfo;
- }
- if($file['type']!=$mimeInfo){
- WLog('FileInfoType','文件类型识别错误'.$file["name"].':'.$mimeInfo);
- }
- $file['extension'] = $this->getExtByMine( $file['mime']);
- $file['type'] = $this->getTypeByMime($file['mime']);
- $file['savepath'] = $customerPath?$savePath:$this->getSaveDir($file['mime']);
- $file['savename'] = $this->getSaveName($file);
- // 自动检查附件
- if($this->autoCheck) {
- if(!$this->check($file))
- return false;
- }
- //保存上传文件
- if(!$this->save($file)) return false;
- if(function_exists($this->hashType)) {
- $fun = $this->hashType;
- $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
- }
- //上传成功后保存文件信息,供其他地方调用
- unset($file['tmp_name'],$file['error']);
- $fileInfo[] = $file;
- $isUpload = true;
- }
- }
-
- if($isUpload) {
- $this->uploadFileInfo = $fileInfo;
- $this->Compress();
- $this->createPreview();
- return true;
- }else{
- //判断是否有base64的文件上传
- $data = input();
- foreach($data as $name=>$base64File){
- preg_match("/^data:(.+);base64,(.+)/",$base64File,$matches);
- $file = [];
- if(count($matches)>0){
- $ext = $this->getExtByMine($matches[1]);
- $fname = time().mt_rand(00000,99999);
- $file['name'] = $fname;
- $file['key'] = $name;
- $file['mime'] = $matches[1];
- $file['extension'] = $this->getExtByMine($matches[1]);
- $file['type'] = $this->getTypeByMime($matches[1]);
- $file['savepath'] = $customerPath?$savePath:$this->getSaveDir($matches[1]);
- $file['savename'] = $fname.".".$ext;
- $f = file_put_contents($file['savepath'].$file['savename'],base64_decode($matches[2]));
- if(function_exists($this->hashType)) {
- $fun = $this->hashType;
- $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
- }
- $file['size'] = filesize($file['savepath']);
- $fileInfo[] = $file;
- }
- }
- if(empty($fileInfo)){
- $this->error = '没有选择上传文件';
- return false;
- }else{
- $this->uploadFileInfo = $fileInfo;
- $this->Compress();
- $this->createPreview();
- return true;
- }
- }
- }
- /**
- * @title: 对$_FILES转换上传文件数组变量为正确的方式
- * @desc: 描述
- * @param {*} $files
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-05-18 17:32:46
- */
- private function dealFiles() {
- $fileArray = array();
- $n = 0;
- foreach ($_FILES as $key=>$file){
- if(is_array($file['name'])) {
- $keys = array_keys($file);
- $count = count($file['name']);
- for ($i=0; $i<$count; $i++) {
- $fileArray[$n]['key'] = $key;
- foreach ($keys as $_key){
- $fileArray[$n][$_key] = $file[$_key][$i];
- }
- $n++;
- }
- }else{
- $fileArray[$key] = $file;
- }
- }
- return $fileArray;
- }
- /**
- * @title: 获取错误代码信息
- * @desc: 描述
- * @param {int} {errorNo} {} {错误编码}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-05-18 17:42:19
- */
- protected function error($errorNo) {
- switch($errorNo) {
- case 1:
- $this->error = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
- break;
- case 2:
- $this->error = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
- break;
- case 3:
- $this->error = '文件只有部分被上传';
- break;
- case 4:
- $this->error = '没有文件被上传';
- break;
- case 6:
- $this->error = '找不到临时文件夹';
- break;
- case 7:
- $this->error = '文件写入失败';
- break;
- default:
- $this->error = '未知上传错误!';
- }
- return ;
- }
- /**
- * @title: 根据上传文件命名规则取得保存文件名
- * @desc: 描述
- * @param {array} {fileinfo} {} {文件信息}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-05-18 17:52:10
- */
- private function getSaveName(array $fileinfo)
- {
- $rule = $this->saveRule;
- if(empty($rule)) {//没有定义命名规则,则保持文件名不变
- $saveName = $fileinfo['name'];
- }else {
- if(function_exists($rule)) {
- //使用函数生成一个唯一文件标识号
- $saveName = $rule(time()).rand(1000,9999).".".$fileinfo['extension'];
- }else {
- //使用给定的文件名作为标识号
- $saveName = $rule.rand(1000,9999).".".$fileinfo['extension'];
- }
- }
- if($this->autoSub) {
- // 使用子目录保存文件
- $fileinfo['savename'] = $saveName;
- $saveName = $this->getSubName($fileinfo).$saveName;
- }
- return $saveName;
- }
- /**
- * 获取子目录的名称
- * @access private
- * @param array $file 上传的文件信息
- * @return string
- */
- private function getSubName($file) {
- switch($this->subType) {
- case 'custom':
- $dir = $this->subDir;
- break;
- case 'date':
- $dir = date($this->dateFormat,time()).DS;
- break;
- case 'hash':
- default:
- $name = md5($file['savename']);
- $dir = '';
- for($i=0;$i<$this->hashLevel;$i++) {
- $dir .= $name[$i].DS;
- }
- break;
- }
- if(!is_dir($file['savepath'].$dir)) {
- mkdir($file['savepath'].$dir,0777,true);
- }
- return $dir;
- }
- /**
- * 检查上传的文件
- * @access private
- * @param array $file 文件信息
- * @return boolean
- */
- private function check($file):bool
- {
- if($file['error']!== 0) {
- //文件上传失败
- //捕获错误代码
- $this->error($file['error']);
- return false;
- }
- //文件上传成功,进行自定义规则检查
- //检查文件大小
- if(!$this->checkSize($file['size'])) {
- $this->error = '上传文件大小不符!';
- return false;
- }
- //检查文件Mime类型
- if(!$this->checkType($file['type'])) {
- $this->error = '上传文件MIME类型不允许!';
- return false;
- }
- //检查文件类型
- if(!$this->checkExt($file['extension'])) {
- $this->error ='上传文件类型'.$file['extension'].'不允许';
- return false;
- }
- //检查是否合法上传
- if(!$this->checkUpload($file['tmp_name'])) {
- $this->error = '非法上传文件!';
- return false;
- }
- return true;
- }
- // 自动转换字符集 支持数组转换
- private function autoCharset($fContents, $from='gbk', $to='utf-8') {
- $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
- $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
- if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
- //如果编码相同或者非字符串标量则不转换
- return $fContents;
- }
- if (function_exists('mb_convert_encoding')) {
- return mb_convert_encoding($fContents, $to, $from);
- } elseif (function_exists('iconv')) {
- return iconv($from, $to, $fContents);
- } else {
- return $fContents;
- }
- }
- /**
- * 检查上传的文件类型是否合法
- * @access private
- * @param string $type 数据
- * @return boolean
- */
- private function checkType($type) {
- if(!empty($this->allowTypes))
- return in_array(strtolower($type),$this->allowTypes);
- return true;
- }
- /**
- * 检查上传的文件后缀是否合法
- * @access private
- * @param string $ext 后缀名
- * @return boolean
- */
- private function checkExt($ext) {
- if(!empty($this->allowExts))
- return in_array(strtolower($ext),$this->allowExts,true);
- return true;
- }
- /**
- * 检查文件大小是否合法
- * @access private
- * @param integer $size 数据
- * @return boolean
- */
- private function checkSize($size) {
- return !($size > $this->maxSize) || (-1 == $this->maxSize);
- }
- /**
- * 检查文件是否非法提交
- * @access private
- * @param string $filename 文件名
- * @return boolean
- */
- private function checkUpload($filename) {
- return is_uploaded_file($filename);
- }
- /**
- * 取得上传文件的后缀
- * @access private
- * @param string $filename 文件名
- * @return boolean
- */
- private function getExt($filename) {
- $pathinfo = pathinfo($filename);
- return $pathinfo['extension'];
- }
- /**
- * 取得上传文件的信息
- * @access public
- * @return array
- */
- public function getUploadFileInfo() {
- return $this->uploadFileInfo;
- }
- /**
- * 取得最后一次错误信息
- * @access public
- * @return string
- */
- public function getErrorMsg() {
- return $this->error;
- }
- /**根据mine类型获取文件后缀 */
- public function getExtByMine($mime=""){
- $mimes = self::$mimes;
- $mime = strtolower($mime);
- $mimes = array_flip($mimes);
- if (array_key_exists($mime, $mimes)) {
- return $mimes[$mime];
- }
- return '';
- }
- /**获取文件类型获取应该存放的文件夹 */
- public function getSaveDir($mime)
- {
- $dir = $this->getTypeByMime($mime);
- $savePath = $this->basePath.$dir.DS.date('Ymd',time()).DS;
- if(!is_dir($savePath) || !is_writeable($savePath)){
- mkdir($savePath,0777,true);
- }
- return $savePath;
- }
- /**根据mime获取文件分类 */
- public function getTypeByMime($mime)
- {
- $dirlist=[
- 'image' =>'images',
- 'face' =>'facepic',
- 'file' =>'files',
- 'audio' =>'audio',
- 'video' =>'video'
- ];
- if(empty($mime))return 'other';
- $type = explode('/',$mime);
- $dir = isset($dirlist[$type[0]])?$dirlist[$type[0]]:'other';
- return $dir;
- }
- /**按文件类型设置上传限制 */
- public function setLimit($ext)
- {
- if(empty($ext))return 'other';
- $mime = isset(self::$mimes[$ext])?self::$mimes[$ext]:'';
- if(empty($mime))return 'other';
- $type = explode('/',$mime);
- if($type=='image' || $type=='face'){
- //读取图片类型的设置
- $this->allowExts=explode(',',$this->config['attach_format_image']);
- $this->maxSize=GetSizeToBit($this->config['attach_format_image_size']);
- }else if($type=='video'){
- //读取视频类型的设置
- $this->allowExts=explode(',',$this->config['attach_format_video']);
- $this->maxSize=GetSizeToBit($this->config['attach_format_video_size']);
- }else if($type=='audio'){
- //读取音频类型的设置
- $this->allowExts=explode(',',$this->config['attach_format_audio']);
- $this->maxSize=GetSizeToBit($this->config['attach_format_audio_size']);
- }else{
- //读取其它类型的设置
- $this->allowExts=explode(',',$this->config['attach_format_file']);
- $this->maxSize=GetSizeToBit($this->config['attach_format_file_size']);
- }
- }
- /**压缩文件 */
- public function Compress()
- {
- if($this->config['Compress']==1){
- foreach($this->uploadFileInfo as &$file){
- if('jpeg'==strtolower($file['extension'])){
- $file['extension'] = 'jpg';
- }
- if($file['type']=='images'){
- $result=\image\Image::convert($file['savepath'].$file['savename'],'',$file['extension'],true);
- \image\Image::ImageCompress($result);
- }
- }
- }
- }
- /**
- * @title: 生成图片文件的预览图
- * @desc: 生成预览图文件的规则为原文件名加上.prev加后缀,如1234.jpg,生成的预览图文件名为1234.jpg.prev.jpg
- * @param {string} {file} {} {原文件绝对路径}
- * @param {string} {size} {80} {预览图最大尺寸(宽度或高度,限制最大的一个),单位像素}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-06-08 10:43:14
- */
- public function createPreview(int $size=80)
- {
- foreach($this->uploadFileInfo as &$file){
- if($file['type']=='images'){
- $url=$file['savepath'].$file['savename'];
- WLog('createPreview',$url);
- \image\Image::ImageCompress($url,$url.'.prev.jpg',$size,50);
- }
- }
- }
- }
|