mirror of
https://github.com/kuaifan/dootask.git
synced 2026-02-04 20:24:14 +00:00
perf: 支持自定义上传图片压缩质量
This commit is contained in:
parent
4f57b195a8
commit
71f13a0b50
@ -553,7 +553,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 (Base::saveContentImage(public_path($tmpPath), base64_decode($text), 90)) {
|
||||
if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) {
|
||||
$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']);
|
||||
$isRep = true;
|
||||
|
||||
@ -41,7 +41,7 @@ class SystemController extends AbstractController
|
||||
* @apiParam {String} type
|
||||
* - get: 获取(默认)
|
||||
* - all: 获取所有(需要管理员权限)
|
||||
* - save: 保存设置(参数:['reg', 'reg_identity', 'reg_invite', 'temp_account_alias', 'login_code', 'password_policy', 'project_invite', 'chat_information', 'anon_message', 'voice2text', 'translation', 'e2e_message', 'auto_archived', 'archived_day', 'task_visible', 'task_default_time', 'all_group_mute', 'all_group_autoin', 'user_private_chat_mute', 'user_group_chat_mute', 'system_alias', 'image_compress', 'image_save_local', 'start_home'])
|
||||
* - save: 保存设置(参数:['reg', 'reg_identity', 'reg_invite', 'temp_account_alias', 'login_code', 'password_policy', 'project_invite', 'chat_information', 'anon_message', 'voice2text', 'translation', 'e2e_message', 'auto_archived', 'archived_day', 'task_visible', 'task_default_time', 'all_group_mute', 'all_group_autoin', 'user_private_chat_mute', 'user_group_chat_mute', 'system_alias', 'image_compress', 'image_quality', 'image_save_local', 'start_home'])
|
||||
|
||||
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
|
||||
* @apiSuccess {String} msg 返回信息(错误描述)
|
||||
@ -80,6 +80,7 @@ class SystemController extends AbstractController
|
||||
'user_group_chat_mute',
|
||||
'system_alias',
|
||||
'image_compress',
|
||||
'image_quality',
|
||||
'image_save_local',
|
||||
'start_home',
|
||||
'file_upload_limit',
|
||||
@ -136,8 +137,6 @@ class SystemController extends AbstractController
|
||||
$setting['all_group_autoin'] = $setting['all_group_autoin'] ?: 'yes';
|
||||
$setting['user_private_chat_mute'] = $setting['user_private_chat_mute'] ?: 'open';
|
||||
$setting['user_group_chat_mute'] = $setting['user_group_chat_mute'] ?: 'open';
|
||||
$setting['image_compress'] = $setting['image_compress'] ?: 'open';
|
||||
$setting['image_save_local'] = $setting['image_save_local'] ?: 'open';
|
||||
$setting['start_home'] = $setting['start_home'] ?: 'close';
|
||||
$setting['file_upload_limit'] = $setting['file_upload_limit'] ?: '';
|
||||
$setting['unclaimed_task_reminder'] = $setting['unclaimed_task_reminder'] ?: 'close';
|
||||
@ -946,7 +945,7 @@ class SystemController extends AbstractController
|
||||
"path" => $path,
|
||||
"fileName" => $fileName,
|
||||
"scale" => $scale,
|
||||
"quality" => 85
|
||||
"quality" => true
|
||||
]);
|
||||
} else {
|
||||
$data = Base::upload([
|
||||
@ -955,7 +954,7 @@ class SystemController extends AbstractController
|
||||
"path" => $path,
|
||||
"fileName" => $fileName,
|
||||
"scale" => $scale,
|
||||
"quality" => 100
|
||||
"quality" => true
|
||||
]);
|
||||
}
|
||||
if (Base::isError($data)) {
|
||||
@ -1091,7 +1090,7 @@ class SystemController extends AbstractController
|
||||
"image64" => $image64,
|
||||
"path" => $path,
|
||||
"fileName" => $fileName,
|
||||
"quality" => 85
|
||||
"quality" => true
|
||||
]);
|
||||
} else {
|
||||
$data = Base::upload([
|
||||
@ -1099,7 +1098,7 @@ class SystemController extends AbstractController
|
||||
"type" => 'file',
|
||||
"path" => $path,
|
||||
"fileName" => $fileName,
|
||||
"quality" => 100
|
||||
"quality" => true
|
||||
]);
|
||||
}
|
||||
//
|
||||
|
||||
@ -322,7 +322,6 @@ class IndexController extends InvokeController
|
||||
"type" => 'publish',
|
||||
"path" => $draftPath,
|
||||
"fileName" => true,
|
||||
"quality" => 100
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@ -146,7 +146,7 @@ class LdapUser extends Model
|
||||
$path = "uploads/user/ldap/";
|
||||
$file = "{$path}{$user->userid}.jpeg";
|
||||
Base::makeDir(public_path($path));
|
||||
if (Base::saveContentImage(public_path($file), $userimg, 90)) {
|
||||
if (Base::saveContentImage(public_path($file), $userimg)) {
|
||||
$user->userimg = $file;
|
||||
}
|
||||
}
|
||||
|
||||
@ -251,7 +251,7 @@ class File extends AbstractModel
|
||||
"type" => 'more',
|
||||
"autoThumb" => false,
|
||||
"path" => $path,
|
||||
"quality" => 100
|
||||
"quality" => true
|
||||
]);
|
||||
if (Base::isError($data)) {
|
||||
throw new ApiException($data['msg']);
|
||||
|
||||
@ -79,7 +79,7 @@ class ProjectTaskContent extends AbstractModel
|
||||
$tmpPath = $path . 'attached/';
|
||||
Base::makeDir(public_path($tmpPath));
|
||||
$tmpPath .= md5($text) . "." . $matchs[1][$key];
|
||||
if (Base::saveContentImage(public_path($tmpPath), base64_decode($text), 90)) {
|
||||
if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) {
|
||||
$paramet = getimagesize(public_path($tmpPath));
|
||||
$content = str_replace($matchs[0][$key], '<img src="{{RemoteURL}}' . $tmpPath . '" original-width="' . $paramet[0] . '" original-height="' . $paramet[1] . '"', $content);
|
||||
}
|
||||
|
||||
@ -47,6 +47,9 @@ class Setting extends AbstractModel
|
||||
switch ($this->name) {
|
||||
case 'system':
|
||||
$value['system_alias'] = $value['system_alias'] ?: env('APP_NAME');
|
||||
$value['image_compress'] = $value['image_compress'] ?: 'open';
|
||||
$value['image_quality'] = min(100, max(0, intval($value['image_quality']) ?: 90));
|
||||
$value['image_save_local'] = $value['image_save_local'] ?: 'open';
|
||||
if (!is_array($value['task_default_time']) || count($value['task_default_time']) != 2 || !Timer::isTime($value['task_default_time'][0]) || !Timer::isTime($value['task_default_time'][1])) {
|
||||
$value['task_default_time'] = ['09:00', '18:00'];
|
||||
}
|
||||
|
||||
@ -807,7 +807,7 @@ class WebSocketDialog extends AbstractModel
|
||||
"image64" => $image64,
|
||||
"path" => $path,
|
||||
"fileName" => $fileName,
|
||||
"quality" => 85
|
||||
"quality" => true
|
||||
]);
|
||||
} else if ($filePath) {
|
||||
Base::makeDir(public_path($path));
|
||||
@ -818,7 +818,7 @@ class WebSocketDialog extends AbstractModel
|
||||
"type" => 'more',
|
||||
"path" => $path,
|
||||
"fileName" => $fileName,
|
||||
"quality" => 100,
|
||||
"quality" => true,
|
||||
"convertVideo" => true
|
||||
]);
|
||||
}
|
||||
|
||||
@ -760,7 +760,7 @@ class WebSocketDialogMsg extends AbstractModel
|
||||
$imagePath = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/";
|
||||
Base::makeDir(public_path($imagePath));
|
||||
$imagePath .= md5s($base64) . "." . $matchs[1][$key];
|
||||
if (Base::saveContentImage(public_path($imagePath), base64_decode($base64), 90)) {
|
||||
if (Base::saveContentImage(public_path($imagePath), base64_decode($base64))) {
|
||||
$imageSize = getimagesize(public_path($imagePath));
|
||||
if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0, 80)) {
|
||||
$imagePath .= "_thumb.{$extension}";
|
||||
@ -844,7 +844,7 @@ class WebSocketDialogMsg extends AbstractModel
|
||||
$image = file_get_contents($str);
|
||||
if (empty($image)) {
|
||||
$text = str_replace($matchs[0][$key], "[:IMAGE:browse:90:90:images/other/imgerr.jpg::]", $text);
|
||||
} else if (Base::saveContentImage(public_path($imagePath), $image, 90)) {
|
||||
} else if (Base::saveContentImage(public_path($imagePath), $image)) {
|
||||
$imageSize = getimagesize(public_path($imagePath));
|
||||
if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0, 80)) {
|
||||
$imagePath .= "_thumb.{$extension}";
|
||||
@ -1012,7 +1012,7 @@ class WebSocketDialogMsg extends AbstractModel
|
||||
$fileUrl = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/" . md5s($msg['thumb']) . ".jpg";
|
||||
$filePath = public_path($fileUrl);
|
||||
Base::makeDir(dirname($filePath));
|
||||
if (!Base::saveContentImage($filePath, $thumb, 90)) {
|
||||
if (!Base::saveContentImage($filePath, $thumb)) {
|
||||
throw new ApiException('保存地图快照失败');
|
||||
}
|
||||
$imageSize = getimagesize($filePath);
|
||||
|
||||
@ -1932,7 +1932,7 @@ class Base
|
||||
// 图片裁剪
|
||||
$cutMode = ($width > 0 && $height > 0) ? 'cover' : 'percentage';
|
||||
$cutMode = $param['scale'][2] ?? $cutMode;
|
||||
Image::thumbImage($array['file'], $array['file'], $width, $height, 90, $cutMode);
|
||||
Image::thumbImage($array['file'], $array['file'], $width, $height, true, $cutMode);
|
||||
// 更新图片尺寸
|
||||
$paramet = getimagesize($array['file']);
|
||||
$array['width'] = $paramet[0];
|
||||
@ -1949,9 +1949,8 @@ class Base
|
||||
}
|
||||
}
|
||||
// 压缩图片
|
||||
$quality = intval($param['quality']);
|
||||
if ($quality > 0) {
|
||||
Image::compressImage($array['file'], $quality);
|
||||
if ($param['quality']) {
|
||||
Image::compressImage($array['file'], $param['quality']);
|
||||
$array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true);
|
||||
}
|
||||
//生成缩略图
|
||||
@ -2177,7 +2176,7 @@ class Base
|
||||
// 图片裁剪
|
||||
$cutMode = ($width > 0 && $height > 0) ? 'cover' : 'percentage';
|
||||
$cutMode = $param['scale'][2] ?? $cutMode;
|
||||
Image::thumbImage($array['file'], $array['file'], $width, $height, 90, $cutMode);
|
||||
Image::thumbImage($array['file'], $array['file'], $width, $height, true, $cutMode);
|
||||
// 更新图片尺寸
|
||||
$paramet = getimagesize($array['file']);
|
||||
$array['width'] = $paramet[0];
|
||||
@ -2194,9 +2193,8 @@ class Base
|
||||
}
|
||||
}
|
||||
// 压缩图片
|
||||
$quality = intval($param['quality']);
|
||||
if ($quality > 0) {
|
||||
Image::compressImage($array['file'], $quality);
|
||||
if ($param['quality']) {
|
||||
Image::compressImage($array['file'], $param['quality']);
|
||||
$array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true);
|
||||
}
|
||||
// 生成缩略图
|
||||
@ -2830,14 +2828,11 @@ class Base
|
||||
* 保存图片到文件(同时压缩)
|
||||
* @param $path
|
||||
* @param $content
|
||||
* @param int $quality 压缩图片质量(默认:0不压缩)
|
||||
* @return bool
|
||||
*/
|
||||
public static function saveContentImage($path, $content, int $quality = 0) {
|
||||
public static function saveContentImage($path, $content) {
|
||||
if (file_put_contents($path, $content)) {
|
||||
if ($quality > 0) {
|
||||
Image::compressImage($path, $quality);
|
||||
}
|
||||
Image::compressImage($path);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@ -220,11 +220,11 @@ class Image
|
||||
* @param string $savePath 保存路径
|
||||
* @param int $width 宽度
|
||||
* @param int $height 高度
|
||||
* @param int $quality 压缩质量(0-100), 0 为不压缩
|
||||
* @param int|bool $quality 压缩质量(0-100), 0 为不压缩,true 为从系统设置里面获取
|
||||
* @param string $mode 模式(percentage|cover|contain)
|
||||
* @return string|null 成功返回图片后缀,失败返回 false
|
||||
*/
|
||||
public static function thumbImage(string $imagePath, string $savePath, int $width, int $height, int $quality = 0, string $mode = 'percentage'): ?string
|
||||
public static function thumbImage(string $imagePath, string $savePath, int $width, int $height, int|bool $quality = 0, string $mode = 'percentage'): ?string
|
||||
{
|
||||
if (!file_exists($imagePath)) {
|
||||
return null;
|
||||
@ -237,7 +237,7 @@ class Image
|
||||
$image = new Image($imagePath);
|
||||
$image->thumb($width, $height, $mode);
|
||||
$image->saveTo($savePath);
|
||||
if ($quality > 0) {
|
||||
if ($quality) {
|
||||
Image::compressImage($savePath, $quality);
|
||||
}
|
||||
if ($savePath != $imagePath && filesize($savePath) >= filesize($imagePath)) {
|
||||
@ -253,14 +253,18 @@ class Image
|
||||
/**
|
||||
* 压缩图片(如果压缩后的图片比原图还大那就直接使用原图)
|
||||
* @param array|string $path 图片路径(如果是数组,第1个元素为原图路径,第2个元素为保存路径)
|
||||
* @param int $quality 压缩质量(0-100)
|
||||
* @param int|bool $quality 压缩质量(0-100),如果为 true,则从系统设置里面获取
|
||||
* @param float $minSize 最小尺寸,小于这个尺寸不压缩(单位:KB)
|
||||
* @return bool
|
||||
*/
|
||||
public static function compressImage(array|string $path, int $quality = 100, float $minSize = 5): bool
|
||||
public static function compressImage(array|string $path, int|bool $quality = true, float $minSize = 5): bool
|
||||
{
|
||||
if (Base::settingFind("system", "image_compress") === 'close') {
|
||||
return false;
|
||||
if ($quality === true) {
|
||||
$setting = Base::setting("system");
|
||||
if ($setting['image_compress'] === 'close') {
|
||||
return false;
|
||||
}
|
||||
$quality = $setting['image_quality'];
|
||||
}
|
||||
if (is_array($path)) {
|
||||
$imagePath = $path[0];
|
||||
@ -272,7 +276,7 @@ class Image
|
||||
if (!file_exists($imagePath)) {
|
||||
return false;
|
||||
}
|
||||
$quality = min(max($quality, 1), 100);
|
||||
$quality = min(max(intval($quality), 1), 100);
|
||||
$imageSize = filesize($imagePath);
|
||||
if ($minSize > 0 && $imageSize < $minSize * 1024) {
|
||||
return false;
|
||||
|
||||
@ -223,6 +223,10 @@
|
||||
<Radio label="close">{{$L('关闭')}}</Radio>
|
||||
</RadioGroup>
|
||||
<div class="form-tip">{{$L('数码相机4M的图片,优化后仅有700KB左右,而且肉眼基本看不出区别。')}}</div>
|
||||
<Input v-model="formDatum.image_quality" style="width:220px;margin-top:6px" :placeholder="$L('默认:90')">
|
||||
<span slot="prepend">{{$L('压缩质量')}}</span>
|
||||
</Input>
|
||||
<div class="form-tip">{{$L('取值范围:0-100,建议:90')}}</div>
|
||||
</FormItem>
|
||||
<FormItem :label="$L('保存网络图片')" prop="image_save_local">
|
||||
<RadioGroup v-model="formDatum.image_save_local">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user