perf: 优化图片上传

This commit is contained in:
kuaifan 2024-11-18 14:33:31 +08:00
parent 42c77db1d4
commit ad3e773f27
13 changed files with 38 additions and 26 deletions

View File

@ -892,10 +892,10 @@ class SystemController extends AbstractController
* @apiParam {String} filename post-文件名 * @apiParam {String} filename post-文件名
* @apiParam {Number} [width] 压缩图片宽默认0 * @apiParam {Number} [width] 压缩图片宽默认0
* @apiParam {Number} [height] 压缩图片高默认0 * @apiParam {Number} [height] 压缩图片高默认0
* @apiParam {String} [whcut] 压缩方式 * @apiParam {String} [whcut] 压缩方式(等比缩放)
* - 1裁切默认宽、高非0有效 * - cover完全覆盖容器可能图片部分不可见width、height必须大于0
* - 0:缩放 * - contain完全装入容器可能容器部分显示空白width、height必须大于0
* - -1'auto':保持等比裁切 * - percentage完全装入容器可能容器有一边尺寸不足默认假如width=200、height=0则宽度最大不超过200、高度自动
* *
* @apiSuccess {Number} ret 返回状态码1正确、0错误 * @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {String} msg 返回信息(错误描述)
@ -908,11 +908,15 @@ class SystemController extends AbstractController
} }
$width = intval(Request::input('width')); $width = intval(Request::input('width'));
$height = intval(Request::input('height')); $height = intval(Request::input('height'));
$whcut = intval(Request::input('whcut', 1)); $whcut = Request::input('whcut');
$scale = [2160, 4160, -1]; $whcut = match (strval($whcut)) {
if ($width > 0 || $height > 0) { '1' => 'cover',
$scale = [$width, $height, $whcut]; '0' => 'contain',
} 'cover',
'contain' => $whcut,
default => 'percentage',
};
$scale = [$width ?: 2160, $height ?: 4160, $whcut];
$path = "uploads/user/picture/" . User::userid() . "/" . date("Ym") . "/"; $path = "uploads/user/picture/" . User::userid() . "/" . date("Ym") . "/";
$image64 = trim(Request::input('image64')); $image64 = trim(Request::input('image64'));
$fileName = trim(Request::input('filename')); $fileName = trim(Request::input('filename'));

View File

@ -2027,7 +2027,7 @@ class Base
if ($width > 0 || $height > 0) { if ($width > 0 || $height > 0) {
$scaleName = "_{WIDTH}x{HEIGHT}"; $scaleName = "_{WIDTH}x{HEIGHT}";
if (isset($param['scale'][2])) { if (isset($param['scale'][2])) {
$scaleName .= "_c{$param['scale'][2]}"; $scaleName .= "_{$param['scale'][2]}";
} }
} }
} }
@ -2065,7 +2065,7 @@ class Base
$data = match ($exif['Orientation']) { $data = match ($exif['Orientation']) {
2 => imageflip($data, IMG_FLIP_HORIZONTAL), 2 => imageflip($data, IMG_FLIP_HORIZONTAL),
3 => imagerotate($data, 180, 0), 3 => imagerotate($data, 180, 0),
4 => imageflip($data, IMG_FLIP_VERTICAL), 4 => imageflip($data, IMG_FLIP_VERTICAL),
5 => imageflip(imagerotate($data, -90, 0), IMG_FLIP_HORIZONTAL), 5 => imageflip(imagerotate($data, -90, 0), IMG_FLIP_HORIZONTAL),
6 => imagerotate($data, -90, 0), 6 => imagerotate($data, -90, 0),
7 => imageflip(imagerotate($data, 90, 0), IMG_FLIP_HORIZONTAL), 7 => imageflip(imagerotate($data, 90, 0), IMG_FLIP_HORIZONTAL),

View File

@ -29,7 +29,7 @@
:headers="uploadHeaders" :headers="uploadHeaders"
:data="uploadParams" :data="uploadParams"
:show-upload-list="false" :show-upload-list="false"
:max-size="maxSize" :max-size="maxImageSize"
:format="['jpg', 'jpeg', 'webp', 'gif', 'png']" :format="['jpg', 'jpeg', 'webp', 'gif', 'png']"
:default-file-list="defaultList" :default-file-list="defaultList"
:on-progress="handleProgress" :on-progress="handleProgress"
@ -77,8 +77,6 @@
</template> </template>
<script> <script>
import {languageList} from "../language";
export default { export default {
name: 'ImgUpload', name: 'ImgUpload',
props: { props: {
@ -101,6 +99,10 @@ export default {
uploadIng: { uploadIng: {
type: Number, type: Number,
default: 0 default: 0
},
maxSize: {
type: Number,
default: 1024 * 10 // 10M
} }
}, },
data() { data() {
@ -118,7 +120,7 @@ export default {
maxNum: Math.min(Math.max($A.runNum(this.num), 1), 99), maxNum: Math.min(Math.max($A.runNum(this.num), 1), 99),
httpValue: '', httpValue: '',
httpType: '', httpType: '',
maxSize: 2048 maxImageSize: this.maxSize
} }
}, },
mounted() { mounted() {
@ -311,7 +313,7 @@ export default {
// //
$A.noticeWarning({ $A.noticeWarning({
title: this.$L('超出文件大小限制'), title: this.$L('超出文件大小限制'),
desc: this.$L('文件 ' + file.name + ' 太大,不能超过:' + $A.bytesToSize(this.maxSize * 1024)) desc: this.$L('文件 ' + file.name + ' 太大,不能超过:' + $A.bytesToSize(this.maxImageSize * 1024))
}); });
}, },
handleBeforeUpload() { handleBeforeUpload() {

View File

@ -18,7 +18,10 @@
type="callback" type="callback"
:uploadIng.sync="uploadIng" :uploadIng.sync="uploadIng"
@on-callback="editorImage" @on-callback="editorImage"
num="50"/> :num="50"
:width="2048"
:height="2048"
whcut="percentage"/>
<Upload <Upload
name="files" name="files"
ref="fileUpload" ref="fileUpload"

View File

@ -22,7 +22,10 @@
type="callback" type="callback"
:uploadIng.sync="uploadIng" :uploadIng.sync="uploadIng"
@on-callback="handleInsertImages" @on-callback="handleInsertImages"
num="50"/> :num="50"
:width="2048"
:height="2048"
whcut="percentage"/>
<Upload <Upload
name="files" name="files"
ref="fileUpload" ref="fileUpload"

View File

@ -256,7 +256,7 @@
:mask-closable="false"> :mask-closable="false">
<Form :model="createGroupData" v-bind="formOptions" @submit.native.prevent> <Form :model="createGroupData" v-bind="formOptions" @submit.native.prevent>
<FormItem prop="avatar" :label="$L('群头像')"> <FormItem prop="avatar" :label="$L('群头像')">
<ImgUpload v-model="createGroupData.avatar" :num="1" :width="512" :height="512" :whcut="1"/> <ImgUpload v-model="createGroupData.avatar" :num="1" :width="512" :height="512" whcut="cover"/>
</FormItem> </FormItem>
<FormItem prop="userids" :label="$L('群成员')"> <FormItem prop="userids" :label="$L('群成员')">
<UserSelect v-model="createGroupData.userids" :uncancelable="createGroupData.uncancelable" :multiple-max="100" show-bot :title="$L('选择项目成员')"/> <UserSelect v-model="createGroupData.userids" :uncancelable="createGroupData.uncancelable" :multiple-max="100" show-bot :title="$L('选择项目成员')"/>

View File

@ -201,7 +201,7 @@
<Input type="textarea" v-model="commentData.content"></Input> <Input type="textarea" v-model="commentData.content"></Input>
</FormItem> </FormItem>
<FormItem prop="pictures" :label="$L('图片')"> <FormItem prop="pictures" :label="$L('图片')">
<ImgUpload v-model="commentData.pictures" :num="3" :width="2000" :height="2000" :whcut="0"></ImgUpload> <ImgUpload v-model="commentData.pictures" :num="3" :width="2048" :height="2048" whcut="percentage"/>
</FormItem> </FormItem>
</Form> </Form>
<div slot="footer" class="adaption"> <div slot="footer" class="adaption">

View File

@ -228,7 +228,7 @@
<Input type="textarea" v-model="addData.description"></Input> <Input type="textarea" v-model="addData.description"></Input>
</FormItem> </FormItem>
<FormItem prop="other" :label="$L('图片')"> <FormItem prop="other" :label="$L('图片')">
<ImgUpload v-model="addData.other" :num="3" :width="2000" :height="2000" :whcut="0"></ImgUpload> <ImgUpload v-model="addData.other" :num="3" :width="2048" :height="2048" whcut="percentage"/>
</FormItem> </FormItem>
</Form> </Form>
<div slot="footer" class="adaption"> <div slot="footer" class="adaption">

View File

@ -20,7 +20,7 @@
:placeholder="$L('请输入填写详细的举报原因,以使我们更好的帮助你解决问题')" /> :placeholder="$L('请输入填写详细的举报原因,以使我们更好的帮助你解决问题')" />
</div> </div>
<div class="group-complaint-img"> <div class="group-complaint-img">
<ImgUpload v-model="imgs" :num="5" :width="512" :height="512" :whcut="1"></ImgUpload> <ImgUpload v-model="imgs" :num="5" :width="2048" :height="2048" whcut="percentage"/>
</div> </div>
</div> </div>
<!-- --> <!-- -->

View File

@ -407,7 +407,7 @@
<Form :model="modifyData" v-bind="formOptions" @submit.native.prevent> <Form :model="modifyData" v-bind="formOptions" @submit.native.prevent>
<Alert v-if="modifyData.system_name" type="error" style="margin-bottom:18px">{{$L(`正在修改系统机器人${modifyData.system_name}`)}}</Alert> <Alert v-if="modifyData.system_name" type="error" style="margin-bottom:18px">{{$L(`正在修改系统机器人${modifyData.system_name}`)}}</Alert>
<FormItem prop="avatar" :label="$L('头像')"> <FormItem prop="avatar" :label="$L('头像')">
<ImgUpload v-model="modifyData.avatar" :num="1" :width="512" :height="512" :whcut="1"/> <ImgUpload v-model="modifyData.avatar" :num="1" :width="512" :height="512" whcut="cover"/>
</FormItem> </FormItem>
<FormItem v-if="typeof modifyData.name !== 'undefined'" prop="name" :label="$L('名称')"> <FormItem v-if="typeof modifyData.name !== 'undefined'" prop="name" :label="$L('名称')">
<Input v-model="modifyData.name" :maxlength="20" /> <Input v-model="modifyData.name" :maxlength="20" />

View File

@ -263,7 +263,7 @@
</Row> </Row>
<Row class="team-department-checkin-item"> <Row class="team-department-checkin-item">
<Col span="24"> <Col span="24">
<ImgUpload v-model="checkinFaceEditData.faceimg" :num="1" :width="512" :height="512" :whcut="1"></ImgUpload> <ImgUpload v-model="checkinFaceEditData.faceimg" :num="1" :width="512" :height="512" whcut="cover"/>
<div class="form-tip">{{$L('建议尺寸500x500')}}</div> <div class="form-tip">{{$L('建议尺寸500x500')}}</div>
</Col> </Col>
</Row> </Row>

View File

@ -26,7 +26,7 @@
</Row> </Row>
<Row class="setting-template"> <Row class="setting-template">
<Col span="24"> <Col span="24">
<ImgUpload v-model="faceimgs" :num="1" :width="512" :height="512" :whcut="1"></ImgUpload> <ImgUpload v-model="faceimgs" :num="1" :width="512" :height="512" whcut="cover"/>
<div class="form-tip">{{ $L('建议尺寸500x500') }}</div> <div class="form-tip">{{ $L('建议尺寸500x500') }}</div>
</Col> </Col>
</Row> </Row>

View File

@ -7,7 +7,7 @@
v-bind="formOptions" v-bind="formOptions"
@submit.native.prevent> @submit.native.prevent>
<FormItem :label="$L('头像')" prop="userimg"> <FormItem :label="$L('头像')" prop="userimg">
<ImgUpload v-model="formData.userimg" :num="1" :width="512" :height="512" :whcut="1"></ImgUpload> <ImgUpload v-model="formData.userimg" :num="1" :width="512" :height="512" whcut="cover"/>
<span class="form-tip">{{$L('建议尺寸200x200')}}</span> <span class="form-tip">{{$L('建议尺寸200x200')}}</span>
</FormItem> </FormItem>
<FormItem :label="$L('邮箱')" prop="email"> <FormItem :label="$L('邮箱')" prop="email">