perf: 优化图片压缩

This commit is contained in:
kuaifan 2024-10-23 14:56:09 +08:00
parent f9ceb3e2d8
commit 7445ac3a39
11 changed files with 92 additions and 53 deletions

View File

@ -553,14 +553,14 @@ class FileController extends AbstractController
$tmpPath = "uploads/file/document/" . date("Ym") . "/" . $id . "/attached/"; $tmpPath = "uploads/file/document/" . date("Ym") . "/" . $id . "/attached/";
Base::makeDir(public_path($tmpPath)); Base::makeDir(public_path($tmpPath));
$tmpPath .= md5($text) . "." . $matchs[1][$key]; $tmpPath .= md5($text) . "." . $matchs[1][$key];
if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) { if (Base::saveContentImage(public_path($tmpPath), base64_decode($text), 90)) {
$paramet = getimagesize(public_path($tmpPath)); $paramet = getimagesize(public_path($tmpPath));
$data['content'] = str_replace($matchs[0][$key], '<img src="' . Base::fillUrl($tmpPath) . '" original-width="' . $paramet[0] . '" original-height="' . $paramet[1] . '"', $data['content']); $data['content'] = str_replace($matchs[0][$key], '<img src="' . Base::fillUrl($tmpPath) . '" original-width="' . $paramet[0] . '" original-height="' . $paramet[1] . '"', $data['content']);
$isRep = true; $isRep = true;
} }
} }
$text = strip_tags($data['content']); $text = strip_tags($data['content']);
if ($isRep == true) { if ($isRep) {
$content = Base::array2json($data); $content = Base::array2json($data);
} }
} }

View File

@ -884,7 +884,8 @@ class SystemController extends AbstractController
"image64" => $image64, "image64" => $image64,
"path" => $path, "path" => $path,
"fileName" => $fileName, "fileName" => $fileName,
"scale" => $scale "scale" => $scale,
"quality" => 85
]); ]);
} else { } else {
$data = Base::upload([ $data = Base::upload([
@ -892,7 +893,8 @@ class SystemController extends AbstractController
"type" => 'image', "type" => 'image',
"path" => $path, "path" => $path,
"fileName" => $fileName, "fileName" => $fileName,
"scale" => $scale "scale" => $scale,
"quality" => 100
]); ]);
} }
if (Base::isError($data)) { if (Base::isError($data)) {
@ -1028,6 +1030,7 @@ class SystemController extends AbstractController
"image64" => $image64, "image64" => $image64,
"path" => $path, "path" => $path,
"fileName" => $fileName, "fileName" => $fileName,
"quality" => 85
]); ]);
} else { } else {
$data = Base::upload([ $data = Base::upload([
@ -1035,6 +1038,7 @@ class SystemController extends AbstractController
"type" => 'file', "type" => 'file',
"path" => $path, "path" => $path,
"fileName" => $fileName, "fileName" => $fileName,
"quality" => 100
]); ]);
} }
// //

View File

@ -197,7 +197,8 @@ class IndexController extends InvokeController
"file" => Request::file('file'), "file" => Request::file('file'),
"type" => 'publish', "type" => 'publish',
"path" => $publishPath, "path" => $publishPath,
"fileName" => true "fileName" => true,
"quality" => 100
]); ]);
if (Base::isSuccess($res)) { if (Base::isSuccess($res)) {
file_put_contents($latestFile, $publishVersion); file_put_contents($latestFile, $publishVersion);

View File

@ -146,7 +146,7 @@ class LdapUser extends Model
$path = "uploads/user/ldap/"; $path = "uploads/user/ldap/";
$file = "{$path}{$user->userid}.jpeg"; $file = "{$path}{$user->userid}.jpeg";
Base::makeDir(public_path($path)); Base::makeDir(public_path($path));
if (Base::saveContentImage(public_path($file), $userimg)) { if (Base::saveContentImage(public_path($file), $userimg, 90)) {
$user->userimg = $file; $user->userimg = $file;
} }
} }

View File

@ -245,14 +245,13 @@ class File extends AbstractModel
} }
} }
// //
$setting = Base::setting('system');
$path = 'uploads/tmp/' . date("Ym") . '/'; $path = 'uploads/tmp/' . date("Ym") . '/';
$data = Base::upload([ $data = Base::upload([
"file" => Request::file('files'), "file" => Request::file('files'),
"type" => 'more', "type" => 'more',
"autoThumb" => false, "autoThumb" => false,
"path" => $path, "path" => $path,
"size" => ($setting['file_upload_limit'] ?: 0) * 1024 "quality" => 100
]); ]);
if (Base::isError($data)) { if (Base::isError($data)) {
throw new ApiException($data['msg']); throw new ApiException($data['msg']);

View File

@ -79,7 +79,7 @@ class ProjectTaskContent extends AbstractModel
$tmpPath = $path . 'attached/'; $tmpPath = $path . 'attached/';
Base::makeDir(public_path($tmpPath)); Base::makeDir(public_path($tmpPath));
$tmpPath .= md5($text) . "." . $matchs[1][$key]; $tmpPath .= md5($text) . "." . $matchs[1][$key];
if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) { if (Base::saveContentImage(public_path($tmpPath), base64_decode($text), 90)) {
$paramet = getimagesize(public_path($tmpPath)); $paramet = getimagesize(public_path($tmpPath));
$content = str_replace($matchs[0][$key], '<img src="{{RemoteURL}}' . $tmpPath . '" original-width="' . $paramet[0] . '" original-height="' . $paramet[1] . '"', $content); $content = str_replace($matchs[0][$key], '<img src="{{RemoteURL}}' . $tmpPath . '" original-width="' . $paramet[0] . '" original-height="' . $paramet[1] . '"', $content);
} }

View File

@ -738,18 +738,18 @@ class WebSocketDialog extends AbstractModel
"image64" => $image64, "image64" => $image64,
"path" => $path, "path" => $path,
"fileName" => $fileName, "fileName" => $fileName,
"quality" => 85
]); ]);
} else if ($filePath) { } else if ($filePath) {
Base::makeDir(public_path($path)); Base::makeDir(public_path($path));
copy($filePath, public_path($path) . basename($filePath)); copy($filePath, public_path($path) . basename($filePath));
} else { } else {
$setting = Base::setting('system');
$data = Base::upload([ $data = Base::upload([
"file" => $files, "file" => $files,
"type" => 'more', "type" => 'more',
"path" => $path, "path" => $path,
"fileName" => $fileName, "fileName" => $fileName,
"size" => ($setting['file_upload_limit'] ?: 0) * 1024, "quality" => 100,
"convertVideo" => true "convertVideo" => true
]); ]);
} }

View File

@ -703,9 +703,9 @@ class WebSocketDialogMsg extends AbstractModel
$imagePath = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/"; $imagePath = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/";
Base::makeDir(public_path($imagePath)); Base::makeDir(public_path($imagePath));
$imagePath .= md5s($base64) . "." . $matchs[1][$key]; $imagePath .= md5s($base64) . "." . $matchs[1][$key];
if (Base::saveContentImage(public_path($imagePath), base64_decode($base64))) { if (Base::saveContentImage(public_path($imagePath), base64_decode($base64), 90)) {
$imageSize = getimagesize(public_path($imagePath)); $imageSize = getimagesize(public_path($imagePath));
if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0)) { if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0, 80)) {
$imagePath .= "_thumb.{$extension}"; $imagePath .= "_thumb.{$extension}";
} }
$text = str_replace($matchs[0][$key], "[:IMAGE:browse:{$imageSize[0]}:{$imageSize[1]}:{$imagePath}::]", $text); $text = str_replace($matchs[0][$key], "[:IMAGE:browse:{$imageSize[0]}:{$imageSize[1]}:{$imagePath}::]", $text);
@ -779,7 +779,7 @@ class WebSocketDialogMsg extends AbstractModel
} }
if (file_exists(public_path($imagePath))) { if (file_exists(public_path($imagePath))) {
$imageSize = getimagesize(public_path($imagePath)); $imageSize = getimagesize(public_path($imagePath));
if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0)) { if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0, 80)) {
$imagePath .= "_thumb.{$extension}"; $imagePath .= "_thumb.{$extension}";
} }
$text = str_replace($matchs[0][$key], "[:IMAGE:browse:{$imageSize[0]}:{$imageSize[1]}:{$imagePath}::]", $text); $text = str_replace($matchs[0][$key], "[:IMAGE:browse:{$imageSize[0]}:{$imageSize[1]}:{$imagePath}::]", $text);
@ -787,9 +787,9 @@ class WebSocketDialogMsg extends AbstractModel
$image = file_get_contents($str); $image = file_get_contents($str);
if (empty($image)) { if (empty($image)) {
$text = str_replace($matchs[0][$key], "[:IMAGE:browse:90:90:images/other/imgerr.jpg::]", $text); $text = str_replace($matchs[0][$key], "[:IMAGE:browse:90:90:images/other/imgerr.jpg::]", $text);
} else if (Base::saveContentImage(public_path($imagePath), $image)) { } else if (Base::saveContentImage(public_path($imagePath), $image, 90)) {
$imageSize = getimagesize(public_path($imagePath)); $imageSize = getimagesize(public_path($imagePath));
if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0)) { if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0, 80)) {
$imagePath .= "_thumb.{$extension}"; $imagePath .= "_thumb.{$extension}";
} }
$text = str_replace($matchs[0][$key], "[:IMAGE:browse:{$imageSize[0]}:{$imageSize[1]}:{$imagePath}::]", $text); $text = str_replace($matchs[0][$key], "[:IMAGE:browse:{$imageSize[0]}:{$imageSize[1]}:{$imagePath}::]", $text);

View File

@ -2126,7 +2126,7 @@ class Base
/** /**
* image64图片保存 * image64图片保存
* @param array $param [ image64=带前缀的base64, path=>文件路径, fileName=>文件名称, scale=>[压缩原图宽,, 压缩方式], autoThumb=>false不要自动生成缩略图, 'compress'=>是否压缩图片(默认true) ] * @param array $param [ image64=带前缀的base64, path=>文件路径, fileName=>文件名称, scale=>[压缩原图宽,, 压缩方式], autoThumb=>false不要自动生成缩略图, 'quality'=>压缩图片质量(默认0不压缩) ]
* @return array [name=>文件名, size=>文件大小(单位KB),file=>绝对地址, path=>相对地址, url=>全路径地址, ext=>文件后缀名] * @return array [name=>文件名, size=>文件大小(单位KB),file=>绝对地址, path=>相对地址, url=>全路径地址, ext=>文件后缀名]
*/ */
public static function image64save($param) public static function image64save($param)
@ -2169,19 +2169,19 @@ class Base
"height" => -1, //图片高度 "height" => -1, //图片高度
"ext" => $extension, //文件后缀名 "ext" => $extension, //文件后缀名
]; ];
//图片尺寸 // 图片尺寸
$paramet = getimagesize($array['file']); $paramet = getimagesize($array['file']);
$array['width'] = $paramet[0]; $array['width'] = $paramet[0];
$array['height'] = $paramet[1]; $array['height'] = $paramet[1];
//原图压缩 // 原图裁剪
if ($param['scale'] && is_array($param['scale'])) { if ($param['scale'] && is_array($param['scale'])) {
list($width, $height) = $param['scale']; list($width, $height) = $param['scale'];
if (($width > 0 && $array['width'] > $width) || ($height > 0 && $array['height'] > $height)) { if (($width > 0 && $array['width'] > $width) || ($height > 0 && $array['height'] > $height)) {
$cut = ($width > 0 && $height > 0) ? 'cover' : 'percentage'; $cut = ($width > 0 && $height > 0) ? 'cover' : 'percentage';
$cut = $param['scale'][2] ?? $cut; $cut = $param['scale'][2] ?? $cut;
//图片压缩 // 图片裁剪
$tmpFile = $array['file'] . '_tmp.jpg'; $tmpFile = $array['file'] . '_tmp.jpg';
if (Image::thumbImage($array['file'], $tmpFile, $width, $height, $cut)) { if (Image::thumbImage($array['file'], $tmpFile, $width, $height, 90, $cut)) {
$tmpSize = filesize($tmpFile); $tmpSize = filesize($tmpFile);
if ($tmpSize > $fileSize) { if ($tmpSize > $fileSize) {
@unlink($tmpFile); @unlink($tmpFile);
@ -2190,11 +2190,11 @@ class Base
rename($tmpFile, $array['file']); rename($tmpFile, $array['file']);
} }
} }
//图片尺寸 // 更新图片尺寸
$paramet = getimagesize($array['file']); $paramet = getimagesize($array['file']);
$array['width'] = $paramet[0]; $array['width'] = $paramet[0];
$array['height'] = $paramet[1]; $array['height'] = $paramet[1];
//重命名 // 重命名
if ($scaleName) { if ($scaleName) {
$scaleName = str_replace(['{WIDTH}', '{HEIGHT}'], [$array['width'], $array['height']], $scaleName); $scaleName = str_replace(['{WIDTH}', '{HEIGHT}'], [$array['width'], $array['height']], $scaleName);
if (rename($array['file'], Base::rightDelete($array['file'], $fileName) . $scaleName)) { if (rename($array['file'], Base::rightDelete($array['file'], $fileName) . $scaleName)) {
@ -2206,8 +2206,9 @@ class Base
} }
} }
// 压缩图片 // 压缩图片
if ($param['compress'] !== false) { $quality = intval($param['quality']);
Image::compressImage($array['file']); if ($quality > 0) {
Image::compressImage($array['file'], null, $quality);
$array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true); $array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true);
} }
//生成缩略图 //生成缩略图
@ -2216,7 +2217,7 @@ class Base
$param['autoThumb'] = false; $param['autoThumb'] = false;
} }
if ($param['autoThumb'] !== false) { if ($param['autoThumb'] !== false) {
if ($extension = Image::thumbImage($array['file'], $array['file'] . "_thumb.{*}", 320, 0)) { if ($extension = Image::thumbImage($array['file'], $array['file'] . "_thumb.{*}", 320, 0, 80)) {
$array['thumb'] .= "_thumb.{$extension}"; $array['thumb'] .= "_thumb.{$extension}";
} }
} }
@ -2238,7 +2239,7 @@ class Base
size=>限制大小KB, size=>限制大小KB,
autoThumb=>false不要自动生成缩略图, autoThumb=>false不要自动生成缩略图,
chmod=>权限(默认0644), chmod=>权限(默认0644),
compress=>是否压缩图片(默认true) , quality=>压缩图片质量(默认0不压缩),
convertVideo=>转换视频格式(默认false) , convertVideo=>转换视频格式(默认false) ,
] ]
* @return array [ * @return array [
@ -2304,10 +2305,15 @@ class Base
if ($type && !in_array($extension, $type)) { if ($type && !in_array($extension, $type)) {
return Base::retError('文件格式错误,限制类型:' . implode(",", $type)); return Base::retError('文件格式错误,限制类型:' . implode(",", $type));
} }
$limitSize = intval($param['size']);
if ($limitSize <= 0) {
$fileUploadLimit = intval(Base::settingFind('system', 'file_upload_limit', 0));
$limitSize = $fileUploadLimit * 1024;
}
try { try {
$fileSize = $file->getSize(); $fileSize = $file->getSize();
if ($param['size'] > 0 && $fileSize > $param['size'] * 1024) { if ($limitSize > 0 && $fileSize > $limitSize * 1024) {
return Base::retError('文件大小超限,最大限制:' . $param['size'] . 'KB'); return Base::retError('文件大小超限,最大限制:' . $limitSize . 'KB');
} }
} catch (\Throwable) { } catch (\Throwable) {
$fileSize = 0; $fileSize = 0;
@ -2353,7 +2359,7 @@ class Base
return Base::retError('上传失败'); return Base::retError('上传失败');
} }
@chmod($array['file'], $chmod); @chmod($array['file'], $chmod);
//iOS照片颠倒处理 // iOS照片颠倒处理
if (in_array($array['ext'], ['jpg', 'jpeg']) && function_exists('exif_read_data')) { if (in_array($array['ext'], ['jpg', 'jpeg']) && function_exists('exif_read_data')) {
$data = imagecreatefromstring(file_get_contents($array['file'])); $data = imagecreatefromstring(file_get_contents($array['file']));
$exif = @exif_read_data($array['file']); $exif = @exif_read_data($array['file']);
@ -2402,19 +2408,19 @@ class Base
} }
} }
if (in_array($array['ext'], ['jpg', 'jpeg', 'webp', 'gif', 'png'])) { if (in_array($array['ext'], ['jpg', 'jpeg', 'webp', 'gif', 'png'])) {
//图片尺寸 // 获取图片尺寸
$paramet = getimagesize($array['file']); $paramet = getimagesize($array['file']);
$array['width'] = $paramet[0]; $array['width'] = $paramet[0];
$array['height'] = $paramet[1]; $array['height'] = $paramet[1];
//原图压缩 // 原图裁剪
if ($param['scale'] && is_array($param['scale'])) { if ($param['scale'] && is_array($param['scale'])) {
list($width, $height) = $param['scale']; list($width, $height) = $param['scale'];
if (($width > 0 && $array['width'] > $width) || ($height > 0 && $array['height'] > $height)) { if (($width > 0 && $array['width'] > $width) || ($height > 0 && $array['height'] > $height)) {
$cut = ($width > 0 && $height > 0) ? 'cover' : 'percentage'; $cut = ($width > 0 && $height > 0) ? 'cover' : 'percentage';
$cut = $param['scale'][2] ?? $cut; $cut = $param['scale'][2] ?? $cut;
//图片压缩 // 图片裁剪
$tmpFile = $array['file'] . '_tmp.jpg'; $tmpFile = $array['file'] . '_tmp.jpg';
if (Image::thumbImage($array['file'], $tmpFile, $width, $height, $cut)) { if (Image::thumbImage($array['file'], $tmpFile, $width, $height, 90, $cut)) {
$tmpSize = filesize($tmpFile); $tmpSize = filesize($tmpFile);
if ($tmpSize > $fileSize) { if ($tmpSize > $fileSize) {
@unlink($tmpFile); @unlink($tmpFile);
@ -2423,11 +2429,11 @@ class Base
rename($tmpFile, $array['file']); rename($tmpFile, $array['file']);
} }
} }
//图片尺寸 // 更新图片尺寸
$paramet = getimagesize($array['file']); $paramet = getimagesize($array['file']);
$array['width'] = $paramet[0]; $array['width'] = $paramet[0];
$array['height'] = $paramet[1]; $array['height'] = $paramet[1];
//重命名 // 重命名
if ($scaleName) { if ($scaleName) {
$scaleName = str_replace(['{WIDTH}', '{HEIGHT}'], [$array['width'], $array['height']], $scaleName); $scaleName = str_replace(['{WIDTH}', '{HEIGHT}'], [$array['width'], $array['height']], $scaleName);
if (rename($array['file'], Base::rightDelete($array['file'], $fileName) . $scaleName)) { if (rename($array['file'], Base::rightDelete($array['file'], $fileName) . $scaleName)) {
@ -2438,21 +2444,22 @@ class Base
} }
} }
} }
//生成缩略图 // 生成缩略图
$array['thumb'] = $array['path']; $array['thumb'] = $array['path'];
if ($array['ext'] === 'gif' && !isset($param['autoThumb'])) { if ($array['ext'] === 'gif' && !isset($param['autoThumb'])) {
$param['autoThumb'] = false; $param['autoThumb'] = false;
} }
if ($param['autoThumb'] !== false) { if ($param['autoThumb'] !== false) {
if ($array['ext'] = Image::thumbImage($array['file'], $array['file'] . "_thumb.{*}", 320, 0)) { if ($array['ext'] = Image::thumbImage($array['file'], $array['file'] . "_thumb.{*}", 320, 0, 80)) {
$array['thumb'] .= "_thumb.{$array['ext']}"; $array['thumb'] .= "_thumb.{$array['ext']}";
} }
} }
$array['thumb'] = Base::fillUrl($array['thumb']); $array['thumb'] = Base::fillUrl($array['thumb']);
} }
// 压缩图片 // 压缩图片
if ($param['compress'] !== false) { $quality = intval($param['quality']);
Image::compressImage($array['file']); if ($quality > 0) {
Image::compressImage($array['file'], null, $quality);
$array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true); $array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true);
} }
// //
@ -3010,13 +3017,13 @@ class Base
* 保存图片到文件(同时压缩) * 保存图片到文件(同时压缩)
* @param $path * @param $path
* @param $content * @param $content
* @param $compress * @param int $quality 压缩图片质量(默认0不压缩)
* @return bool * @return bool
*/ */
public static function saveContentImage($path, $content, $compress = true) { public static function saveContentImage($path, $content, int $quality = 0) {
if (file_put_contents($path, $content)) { if (file_put_contents($path, $content)) {
if ($compress) { if ($quality > 0) {
Image::compressImage($path); Image::compressImage($path, null, $quality);
} }
return true; return true;
} }

View File

@ -171,10 +171,11 @@ class Image
* @param string $savePath 保存路径 * @param string $savePath 保存路径
* @param int $width 宽度 * @param int $width 宽度
* @param int $height 高度 * @param int $height 高度
* @param int $quality 压缩质量0-100, 0 为不压缩
* @param string $mode 模式percentage|cover|contain * @param string $mode 模式percentage|cover|contain
* @return string|null 成功返回图片后缀,失败返回 false * @return string|null 成功返回图片后缀,失败返回 false
*/ */
public static function thumbImage(string $imagePath, string $savePath, int $width, int $height, string $mode = 'percentage'): ?string public static function thumbImage(string $imagePath, string $savePath, int $width, int $height, int $quality = 0, string $mode = 'percentage'): ?string
{ {
if (!file_exists($imagePath)) { if (!file_exists($imagePath)) {
return null; return null;
@ -187,6 +188,9 @@ class Image
$image = new Image($imagePath); $image = new Image($imagePath);
$image->thumb($width, $height, $mode); $image->thumb($width, $height, $mode);
$image->saveTo($savePath); $image->saveTo($savePath);
if ($quality > 0) {
Image::compressImage($savePath, null, $quality);
}
return $extension; return $extension;
} catch (\ImagickException) { } catch (\ImagickException) {
return null; return null;
@ -194,14 +198,14 @@ class Image
} }
/** /**
* 压缩图片 * 压缩图片(如果压缩后的图片比原图还大那就直接使用原图)
* @param string $imagePath 图片路径 * @param string $imagePath 图片路径
* @param string|null $savePath 保存路径(默认覆盖原图) * @param string|null $savePath 保存路径(默认覆盖原图)
* @param int $quality 压缩质量0-100 * @param int $quality 压缩质量0-100
* @param float $minSize 最小尺寸单位KB * @param float $minSize 最小尺寸,小于这个尺寸不压缩单位KB
* @return bool * @return bool
*/ */
public static function compressImage(string $imagePath, string $savePath = null, int $quality = 100, float $minSize = 10): bool public static function compressImage(string $imagePath, string $savePath = null, int $quality = 100, float $minSize = 5): bool
{ {
if (Base::settingFind("system", "image_compress") === 'close') { if (Base::settingFind("system", "image_compress") === 'close') {
return false; return false;
@ -209,6 +213,7 @@ class Image
if (!file_exists($imagePath)) { if (!file_exists($imagePath)) {
return false; return false;
} }
$quality = min(max($quality, 1), 100);
$imageSize = filesize($imagePath); $imageSize = filesize($imagePath);
if ($minSize > 0 && $imageSize < $minSize * 1024) { if ($minSize > 0 && $imageSize < $minSize * 1024) {
return false; return false;
@ -217,10 +222,7 @@ class Image
$savePath = $imagePath; $savePath = $imagePath;
} }
$tmpPath = $imagePath . '.compress.tmp'; $tmpPath = $imagePath . '.compress.tmp';
try { if (self::compressAuto($imagePath, $tmpPath, $quality)) {
$image = new Image($imagePath);
$image->compress($quality);
$image->saveTo($tmpPath);
if (filesize($tmpPath) >= $imageSize) { if (filesize($tmpPath) >= $imageSize) {
copy($imagePath, $savePath); copy($imagePath, $savePath);
unlink($tmpPath); unlink($tmpPath);
@ -228,6 +230,32 @@ class Image
rename($tmpPath, $savePath); rename($tmpPath, $savePath);
} }
return true; return true;
}
return false;
}
/**
* 自动压缩图片仅限于compressImage方法使用
* @param string $imagePath
* @param string $savePath
* @param int $quality
* @return bool
*/
private static function compressAuto(string $imagePath, string $savePath, int $quality = 100): bool
{
if (strtolower(pathinfo($imagePath, PATHINFO_EXTENSION)) === 'png') {
$minQuality = $quality - 20;
$compressedContent = shell_exec("pngquant --quality={$minQuality}-{$quality} --strip - < " . $imagePath);
if ($compressedContent) {
file_put_contents($savePath, $compressedContent);
return true;
}
}
try {
$image = new Image($imagePath);
$image->compress($quality);
$image->saveTo($savePath);
return true;
} catch (\ImagickException) { } catch (\ImagickException) {
return false; return false;
} }

View File

@ -1,7 +1,7 @@
services: services:
php: php:
container_name: "dootask-php-${APP_ID}" container_name: "dootask-php-${APP_ID}"
image: "kuaifan/php:swoole-8.0.rc15" image: "kuaifan/php:swoole-8.0.rc16"
shm_size: "2gb" shm_size: "2gb"
ulimits: ulimits:
core: core: