perf: 对webp文件的支持

This commit is contained in:
kuaifan 2023-04-06 10:34:23 +08:00
parent 433a46bba9
commit 8ffac1376a
16 changed files with 49 additions and 42 deletions

View File

@ -607,7 +607,7 @@ class FileController extends AbstractController
if ($file->type == 'document') {
$data = Base::json2array($content);
$isRep = false;
preg_match_all("/<img\s+src=\"data:image\/(png|jpg|jpeg);base64,(.*?)\"/s", $data['content'], $matchs);
preg_match_all("/<img\s+src=\"data:image\/(png|jpg|jpeg|webp);base64,(.*?)\"/s", $data['content'], $matchs);
foreach ($matchs[2] as $key => $text) {
$tmpPath = "uploads/file/document/" . date("Ym") . "/" . $id . "/attached/";
Base::makeDir(public_path($tmpPath));
@ -811,7 +811,7 @@ class FileController extends AbstractController
'xls', 'xlsx' => "excel",
'ppt', 'pptx' => "ppt",
'wps' => "wps",
'jpg', 'jpeg', 'png', 'gif', 'bmp', 'ico', 'raw', 'svg' => "picture",
'jpg', 'jpeg', 'webp', 'png', 'gif', 'bmp', 'ico', 'raw', 'svg' => "picture",
'rar', 'zip', 'jar', '7-zip', 'tar', 'gzip', '7z', 'gz', 'apk', 'dmg' => "archive",
'tif', 'tiff' => "tif",
'dwg', 'dxf' => "cad",

View File

@ -783,7 +783,7 @@ class SystemController extends AbstractController
];
//
$extension = pathinfo($dirPath . $filename, PATHINFO_EXTENSION);
if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png', 'bmp'))) {
if (in_array($extension, array('gif', 'jpg', 'jpeg', 'webp', 'png', 'bmp'))) {
if (file_exists($dirPath . $filename . '_thumb.jpg')) {
$array['thumb'] .= '_thumb.jpg';
}

View File

@ -4,7 +4,6 @@ namespace App\Ldap;
use App\Models\User;
use App\Module\Base;
use App\Module\ImgCompress;
use LdapRecord\Configuration\ConfigurationException;
use LdapRecord\Container;
use LdapRecord\LdapRecordException;

View File

@ -81,14 +81,14 @@ class File extends AbstractModel
* 图片文件
*/
const imageExt = [
'jpg', 'jpeg', 'png', 'gif', 'bmp'
'jpg', 'jpeg', 'webp', 'png', 'gif', 'bmp'
];
/**
* 本地媒体文件
*/
const localExt = [
'jpg', 'jpeg', 'png', 'gif', 'bmp', 'ico', 'raw',
'jpg', 'jpeg', 'webp', 'png', 'gif', 'bmp', 'ico', 'raw',
'tif', 'tiff',
'mp3', 'wav', 'mp4', 'flv',
'avi', 'mov', 'wmv', 'mkv', '3gp', 'rm',

View File

@ -61,7 +61,7 @@ class ProjectTaskContent extends AbstractModel
{
$path = 'uploads/task/content/' . date("Ym") . '/' . $task_id . '/';
//
preg_match_all("/<img\s+src=\"data:image\/(png|jpg|jpeg);base64,(.*?)\"/s", $content, $matchs);
preg_match_all("/<img\s+src=\"data:image\/(png|jpg|jpeg|webp);base64,(.*?)\"/s", $content, $matchs);
foreach ($matchs[2] as $key => $text) {
$tmpPath = $path . 'attached/';
Base::makeDir(public_path($tmpPath));

View File

@ -94,7 +94,7 @@ class ProjectTaskFile extends AbstractModel
if (!isset($this->appendattrs['width'])) {
$width = -1;
$height = -1;
if (in_array($this->ext, ['jpg', 'jpeg', 'gif', 'png'])) {
if (in_array($this->ext, ['jpg', 'jpeg', 'webp', 'gif', 'png'])) {
$path = public_path($this->getRawOriginal('path'));
[$width, $height] = Cache::remember("File::size-" . md5($path), now()->addDays(7), function () use ($path) {
$width = -1;

View File

@ -2,9 +2,7 @@
namespace App\Models;
use App\Module\ImgCompress;
use Carbon\Carbon;
use App\Models\User;
use App\Module\Base;
use App\Tasks\PushTask;
use App\Exceptions\ApiException;
@ -127,7 +125,7 @@ class WebSocketDialogMsg extends AbstractModel
}
$value = Base::json2array($value);
if ($this->type === 'file') {
$value['type'] = in_array($value['ext'], ['jpg', 'jpeg', 'png', 'gif']) ? 'img' : 'file';
$value['type'] = in_array($value['ext'], ['jpg', 'jpeg', 'webp', 'png', 'gif']) ? 'img' : 'file';
$value['path'] = Base::fillUrl($value['path']);
$value['thumb'] = Base::fillUrl($value['thumb'] ?: Base::extIcon($value['ext']));
} else if ($this->type === 'record') {
@ -582,7 +580,7 @@ class WebSocketDialogMsg extends AbstractModel
// 基础处理
$text = preg_replace("/<(\/[a-zA-Z]+)\s*>/s", "<$1>", $text);
// 图片 [:IMAGE:className:width:height:src:alt:]
preg_match_all("/<img\s+src=\"data:image\/(png|jpg|jpeg|gif);base64,(.*?)\"(.*?)>(<\/img>)*/s", $text, $matchs);
preg_match_all("/<img\s+src=\"data:image\/(png|jpg|jpeg|webp|gif);base64,(.*?)\"(.*?)>(<\/img>)*/s", $text, $matchs);
foreach ($matchs[2] as $key => $base64) {
$imagePath = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/";
Base::makeDir(public_path($imagePath));
@ -618,7 +616,7 @@ class WebSocketDialogMsg extends AbstractModel
$imageSize = getimagesize(public_path($imagePath));
// 添加后缀
if ($imageSize && !str_contains($imagePath, '.')) {
preg_match("/^image\/(png|jpg|jpeg|gif)$/", $imageSize['mime'], $matchMine);
preg_match("/^image\/(png|jpg|jpeg|webp|gif)$/", $imageSize['mime'], $matchMine);
if ($matchMine) {
$imageNewPath = $imagePath . "." . $matchMine[1];
if (rename(public_path($imagePath), public_path($imageNewPath))) {
@ -642,7 +640,7 @@ class WebSocketDialogMsg extends AbstractModel
}
// 其他网络图片
$imageSaveLocal = Base::settingFind("system", "image_save_local");
preg_match_all("/<img[^>]*?src=([\"'])(.*?\.(png|jpg|jpeg|gif))\\1[^>]*?>/is", $text, $matchs);
preg_match_all("/<img[^>]*?src=([\"'])(.*?\.(png|jpg|jpeg|webp|gif))\\1[^>]*?>/is", $text, $matchs);
foreach ($matchs[2] as $key => $str) {
if ($imageSaveLocal === 'close') {
$imageSize = getimagesize($str);
@ -801,7 +799,7 @@ class WebSocketDialogMsg extends AbstractModel
}
}
} elseif ($type === 'file') {
if (in_array($msg['ext'], ['jpg', 'jpeg', 'png', 'gif'])) {
if (in_array($msg['ext'], ['jpg', 'jpeg', 'webp', 'png', 'gif'])) {
$mtype = 'image';
}
}

View File

@ -2085,7 +2085,7 @@ class Base
$imgBase64 = $param['image64'];
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgBase64, $res)) {
$extension = $res[2];
if (!in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
if (!in_array($extension, ['png', 'jpg', 'jpeg', 'webp', 'gif'])) {
return Base::retError('图片格式错误');
}
$scaleName = "";
@ -2197,11 +2197,8 @@ class Base
case 'png':
$type = ['png'];
break;
case 'png+jpg':
$type = ['jpg', 'jpeg', 'png'];
break;
case 'image':
$type = ['jpg', 'jpeg', 'gif', 'png'];
$type = ['jpg', 'jpeg', 'webp', 'gif', 'png'];
break;
case 'video':
$type = ['rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4'];
@ -2219,7 +2216,7 @@ class Base
$type = ['zip'];
break;
case 'file':
$type = ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'];
$type = ['jpg', 'jpeg', 'webp', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'];
break;
case 'firmware':
$type = ['img', 'tar', 'bin'];
@ -2306,7 +2303,7 @@ class Base
}
}
//
if (in_array($extension, ['jpg', 'jpeg', 'gif', 'png'])) {
if (in_array($extension, ['jpg', 'jpeg', 'webp', 'gif', 'png'])) {
//图片尺寸
$paramet = getimagesize($array['file']);
$array['width'] = $paramet[0];
@ -2417,7 +2414,7 @@ class Base
$dst_img = $src_img;
}
$st = pathinfo($src_img, PATHINFO_EXTENSION);
if (!in_array(strtolower($st), array('jpg', 'jpeg', 'png', 'gif', 'bmp'))) {
if (!in_array(strtolower($st), array('jpg', 'jpeg', 'webp', 'png', 'gif', 'bmp'))) {
return false;
}
$ot = pathinfo($dst_img, PATHINFO_EXTENSION);

View File

@ -34,7 +34,9 @@ class ImgCompress
*/
public function compressImg($saveName = '')
{
$this->_openImage();
if (!$this->_openImage()) { //打开图片
return;
}
if (!empty($saveName)) $this->_saveImage($saveName); //保存
else $this->_showImage();
}
@ -52,8 +54,12 @@ class ImgCompress
'attr' => $attr
);
$fun = "imagecreatefrom" . $this->imageinfo['type'];
if (!function_exists($fun)) {
return false;
}
$this->image = $fun($this->src);
$this->_thumpImage();
return true;
}
/**
@ -90,7 +96,7 @@ class ImgCompress
if (str_contains($dstImgName, '.')) {
$dstName = $dstImgName;
} else {
$allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp', '.gif']; //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名
$allowImgs = ['.jpg', '.jpeg', '.webp', '.png', '.bmp', '.wbmp', '.gif']; //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名
$dstExt = strrchr($dstImgName, ".");
$sourseExt = strrchr($this->src, ".");
if (!empty($dstExt)) $dstExt = strtolower($dstExt);
@ -105,6 +111,9 @@ class ImgCompress
}
}
$funcs = "image" . $this->imageinfo['type'];
if (!function_exists($funcs)) {
return false;
}
$funcs($this->image, $dstName);
return true;
}
@ -114,7 +123,9 @@ class ImgCompress
*/
public function __destruct()
{
imagedestroy($this->image);
if ($this->image) {
imagedestroy($this->image);
}
}
/**
@ -132,7 +143,7 @@ class ImgCompress
if (!file_exists($src)) {
return;
}
$allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp'];
$allowImgs = ['.jpg', '.jpeg', '.webp', '.png', '.bmp', '.wbmp'];
if (!in_array(strrchr($src, "."), $allowImgs)) {
return;
}

View File

@ -179,7 +179,7 @@ export default {
"tar|zip|7z|rar|gz|arj|z"
],
"images": [
"icon|jpg|jpeg|png|bmp|gif|tif|emf"
"icon|jpg|jpeg|webp|png|bmp|gif|tif|emf"
]
},
}),

View File

@ -30,7 +30,7 @@
:data="uploadParams"
:show-upload-list="false"
:max-size="maxSize"
:format="['jpg', 'jpeg', 'gif', 'png']"
:format="['jpg', 'jpeg', 'webp', 'gif', 'png']"
:default-file-list="defaultList"
:on-progress="handleProgress"
:on-success="handleSuccess"
@ -259,7 +259,7 @@
//
$A.noticeWarning({
title: this.$L('文件格式不正确'),
desc: this.$L('文件 ' + file.name + ' 格式不正确,请上传 jpg、jpeg、gif、png 格式的图片。')
desc: this.$L('文件 ' + file.name + ' 格式不正确,请上传 jpg、jpeg、webp、gif、png 格式的图片。')
});
},
handleMaxSize (file) {

View File

@ -157,7 +157,7 @@
htmlValue: '',
uploadIng: 0,
uploadFormat: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'],
uploadFormat: ['jpg', 'jpeg', 'webp', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'],
actionUrl: $A.apiUrl('system/fileupload'),
maxSize: 1024000
};

View File

@ -146,7 +146,7 @@
transfer: false,
uploadIng: 0,
uploadFormat: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'],
uploadFormat: ['jpg', 'jpeg', 'webp', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'],
actionUrl: $A.apiUrl('system/fileupload'),
maxSize: 10240
};

View File

@ -2383,7 +2383,7 @@ export default {
data = this.operateItem
}
const {msg} = data;
if (['jpg', 'jpeg', 'gif', 'png'].includes(msg.ext)) {
if (['jpg', 'jpeg', 'webp', 'gif', 'png'].includes(msg.ext)) {
this.onViewPicture(msg.path);
return
}
@ -2424,7 +2424,7 @@ export default {
onViewPicture(currentUrl) {
const data = this.allMsgs.filter(item => {
if (item.type === 'file') {
return ['jpg', 'jpeg', 'gif', 'png'].includes(item.msg.ext);
return ['jpg', 'jpeg', 'webp', 'gif', 'png'].includes(item.msg.ext);
} else if (item.type === 'text') {
return item.msg.text.match(/<img\s+class="browse"[^>]*?>/);
}
@ -2529,12 +2529,12 @@ export default {
this.$store.dispatch("call", {
url: 'dialog/msg/emoji',
data,
}).then(({data}) => {
const index = this.dialogMsgs.findIndex(item => item.id == data.id)
}).then(({data: resData}) => {
const index = this.dialogMsgs.findIndex(item => item.id == resData.id)
if (index > -1) {
this.$store.dispatch("saveDialogMsg", data);
} else if (this.todoViewData.id === data.id) {
this.todoViewData = Object.assign(this.todoViewData, data)
this.$store.dispatch("saveDialogMsg", resData);
} else if (this.todoViewData.id === resData.id) {
this.todoViewData = Object.assign(this.todoViewData, resData)
}
}).catch(({msg}) => {
$A.messageError(msg);
@ -2758,6 +2758,8 @@ export default {
let format = "png";
if ($A.rightExists(url, "jpg") || $A.rightExists(url, "jpeg")) {
format = "jpeg"
} else if ($A.rightExists(url, "webp")) {
format = "webp"
} else if ($A.rightExists(url, "git")) {
format = "git"
}

View File

@ -1450,8 +1450,8 @@ export default {
},
viewFile(file) {
if (['jpg', 'jpeg', 'gif', 'png'].includes(file.ext)) {
const list = this.fileList.filter(item => ['jpg', 'jpeg', 'gif', 'png'].includes(item.ext))
if (['jpg', 'jpeg', 'webp', 'gif', 'png'].includes(file.ext)) {
const list = this.fileList.filter(item => ['jpg', 'jpeg', 'webp', 'gif', 'png'].includes(item.ext))
const index = list.findIndex(item => item.id === file.id);
if (index > -1) {
this.$store.dispatch("previewImage", {

View File

@ -25,7 +25,7 @@ export default {
data() {
return {
uploadFormat: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'],
uploadFormat: ['jpg', 'jpeg', 'webp', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'pr', 'psd', 'svg', 'tif'],
}
},