diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index 4333fe177..02afb1c67 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -15,7 +15,6 @@ use App\Models\WebSocketDialogMsgRead; use App\Models\WebSocketDialogMsgTodo; use App\Models\WebSocketDialogUser; use App\Module\Base; -use App\Module\ImgCompress; use App\Module\TimeRange; use Carbon\Carbon; use DB; @@ -854,7 +853,6 @@ class DialogController extends AbstractController $fileData = $data['data']; $fileData['thumb'] = Base::unFillUrl($fileData['thumb']); $fileData['size'] *= 1024; - ImgCompress::compress($fileData['file']); // if ($dialog->type === 'group' && $dialog->group_type === 'task') { // 任务群组保存文件 if ($image_attachment || !in_array($fileData['ext'], File::imageExt)) { // 如果是图片不保存 diff --git a/app/Http/Controllers/Api/FileController.php b/app/Http/Controllers/Api/FileController.php index 44dc49e6c..8d9364511 100755 --- a/app/Http/Controllers/Api/FileController.php +++ b/app/Http/Controllers/Api/FileController.php @@ -612,7 +612,7 @@ class FileController extends AbstractController $tmpPath = "uploads/file/document/" . date("Ym") . "/" . $id . "/attached/"; Base::makeDir(public_path($tmpPath)); $tmpPath .= md5($text) . "." . $matchs[1][$key]; - if (file_put_contents(public_path($tmpPath), base64_decode($text))) { + if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) { $paramet = getimagesize(public_path($tmpPath)); $data['content'] = str_replace($matchs[0][$key], 'getPhoto(); if ($userimg) { $path = "uploads/user/ldap/"; + $file = "{$path}{$user->userid}.jpeg"; Base::makeDir(public_path($path)); - file_put_contents(public_path("{$path}{$user->userid}.jpeg"), $userimg); - $user->userimg = "{$path}{$user->userid}.jpeg"; + if (Base::saveContentImage(public_path($file), $userimg)) { + $user->userimg = $file; + } } $user->nickname = $row->getDisplayName(); $user->save(); diff --git a/app/Models/ProjectTaskContent.php b/app/Models/ProjectTaskContent.php index 9e1809b1a..568a41663 100644 --- a/app/Models/ProjectTaskContent.php +++ b/app/Models/ProjectTaskContent.php @@ -66,7 +66,7 @@ class ProjectTaskContent extends AbstractModel $tmpPath = $path . 'attached/'; Base::makeDir(public_path($tmpPath)); $tmpPath .= md5($text) . "." . $matchs[1][$key]; - if (file_put_contents(public_path($tmpPath), base64_decode($text))) { + if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) { $paramet = getimagesize(public_path($tmpPath)); $content = str_replace($matchs[0][$key], '文件路径, fileName=>文件名称, scale=>[压缩原图宽,高, 压缩方式], autoThumb=>false不要自动生成缩略图 ] + * @param array $param [ image64=带前缀的base64, path=>文件路径, fileName=>文件名称, scale=>[压缩原图宽,高, 压缩方式], autoThumb=>false不要自动生成缩略图, 'compress'=>是否压缩图片(默认true) ] * @return array [name=>文件名, size=>文件大小(单位KB),file=>绝对地址, path=>相对地址, url=>全路径地址, ext=>文件后缀名] */ public static function image64save($param) @@ -2156,6 +2156,11 @@ class Base } } } + // 压缩图片 + if ($param['compress'] !== false) { + ImgCompress::compress($array['file']); + $array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true); + } //生成缩略图 $array['thumb'] = $array['path']; if ($extension === 'gif' && !isset($param['autoThumb'])) { @@ -2175,7 +2180,7 @@ class Base /** * 上传文件 - * @param array $param [ type=[文件类型], file=>Request::file, path=>文件路径, fileName=>文件名称, scale=>[压缩原图宽,高, 压缩方式], size=>限制大小KB, autoThumb=>false不要自动生成缩略图, chmod=>权限(默认0644) ] + * @param array $param [ type=[文件类型], file=>Request::file, path=>文件路径, fileName=>文件名称, scale=>[压缩原图宽,高, 压缩方式], size=>限制大小KB, autoThumb=>false不要自动生成缩略图, chmod=>权限(默认0644), 'compress'=>是否压缩图片(默认true) ] * @return array [name=>原文件名, size=>文件大小(单位KB),file=>绝对地址, path=>相对地址, url=>全路径地址, ext=>文件后缀名] */ public static function upload($param) @@ -2350,6 +2355,11 @@ class Base } $array['thumb'] = Base::fillUrl($array['thumb']); } + // 压缩图片 + if ($param['compress'] !== false) { + ImgCompress::compress($array['file']); + $array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true); + } // return Base::retSuccess('success', $array); } else { @@ -2961,4 +2971,21 @@ class Base } return Response::streamDownload($callback, $name); } + + /** + * 保存图片到文件(同时压缩) + * @param $path + * @param $content + * @param $compress + * @return bool + */ + public static function saveContentImage($path, $content, $compress = true) { + if (file_put_contents($path, $content)) { + if ($compress) { + ImgCompress::compress($path); + } + return true; + } + return false; + } } diff --git a/app/Module/ImgCompress.php b/app/Module/ImgCompress.php index d03362561..d1ef6e489 100644 --- a/app/Module/ImgCompress.php +++ b/app/Module/ImgCompress.php @@ -129,7 +129,7 @@ class ImgCompress if (!file_exists($src)) { return; } - $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp', '.gif']; + $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp']; if (!in_array(strrchr($src, "."), $allowImgs)) { return; }