update: 自动裁切正方形

This commit is contained in:
mixtan 2025-09-05 16:02:12 +08:00
parent 7713cc1243
commit 9926c4b5cb
2 changed files with 124 additions and 9 deletions

View File

@ -88,6 +88,13 @@
>
{{ tipObj.prompt }}
</view>
<!-- 用于裁剪的canvas默认隐藏 -->
<canvas
canvas-id="cropperCanvas"
class="cropper-canvas"
:style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
></canvas>
</view>
</template>
@ -167,7 +174,7 @@ export default {
pressImg: {
// H5api
type: Number,
default: -1,
default: 75,
},
config: {
type: Object,
@ -207,6 +214,11 @@ export default {
headers: {},
curPlatform: "",
currIndex: null,
croppedImageUrl: "", //
canvasWidth: 0, // canvas
canvasHeight: 0, // canvas
tempImagePath: "", //
};
},
created() {
@ -251,6 +263,88 @@ export default {
},
},
methods: {
//
processImage(tempImagePath) {
return new Promise((reolse, reject) => {
uni.getImageInfo({
src: tempImagePath,
success: (imageInfo) => {
//
const { width, height } = imageInfo;
//
const sideLength = Math.min(width, height);
//
const x = (width - sideLength) / 2;
const y = (height - sideLength) / 2;
// canvas
this.canvasWidth = sideLength;
this.canvasHeight = sideLength;
// canvas
const ctx = uni.createCanvasContext("cropperCanvas", this);
//
ctx.clearRect(0, 0, sideLength, sideLength);
//
ctx.drawImage(
tempImagePath,
x,
y,
sideLength,
sideLength, //
0,
0,
sideLength,
sideLength //
);
//
ctx.draw(false, () => {
// canvas
uni.canvasToTempFilePath(
{
canvasId: "cropperCanvas",
width: sideLength,
height: sideLength,
destWidth: sideLength,
destHeight: sideLength,
quality: 0.9,
success: (res) => {
// this.croppedImageUrl = res.tempFilePath;
reolse(res.tempFilePath);
// uni.showToast({
// title: "",
// icon: "success",
// });
},
fail: (err) => {
reject(err);
console.error("canvas转图片失败", err);
// uni.showToast({
// title: "",
// icon: "none",
// });
},
},
this
);
});
},
fail: (err) => {
reject(err);
console.error("获取图片信息失败:", err);
// uni.showToast({
// title: "",
// icon: "none",
// });
},
});
});
},
upPhoto() {
let that = this;
if (!that.url) {
@ -275,8 +369,8 @@ export default {
uni.chooseImage({
count: Number(that.canUpCount),
sizeType: ["original", "compressed"], //
sourceType: ["camera"], //
success: function (res) {
sourceType: ["camera"], //
success: async function (res) {
if (res) {
if (res.tempFiles) {
for (let item of res.tempFiles) {
@ -303,7 +397,13 @@ export default {
}
}
that.currIndex = that.imgArray.length;
that.imgArray = [...that.imgArray, ...res.tempFilePaths];
//#ifdef H5
let tempUrl = res.tempFilePaths[0];
//#endif
//#ifdef APP-PLUS
let tempUrl = await that.processImage(res.tempFilePaths[0]);
//#endif
that.imgArray = [...that.imgArray, tempUrl];
that.changeImgArr(false);
if (that.pressImg >= 0) {
//
@ -323,7 +423,7 @@ export default {
count: Number(that.canUpCount),
sizeType: ["original", "compressed"], //
sourceType: ["album"], //
success: function (res) {
success: async function (res) {
if (res) {
if (res.tempFiles) {
for (let item of res.tempFiles) {
@ -350,7 +450,14 @@ export default {
}
}
that.currIndex = that.imgArray.length;
that.imgArray = [...that.imgArray, ...res.tempFilePaths];
//#ifdef H5
let tempUrl = res.tempFilePaths[0];
//#endif
//#ifdef APP-PLUS
let tempUrl = await that.processImage(res.tempFilePaths[0]);
//#endif
that.imgArray = [...that.imgArray, tempUrl];
that.changeImgArr(false);
if (that.pressImg >= 0) {
//
@ -418,7 +525,7 @@ export default {
uni.compressImage({
src: item.url,
quality: _this.pressImg,
success: (res) => {
success: async (res) => {
++success;
item.pressUrl = res.tempFilePath;
},
@ -498,8 +605,8 @@ export default {
//
_this.imgArray[i].result = true;
res.code = 0;
_this.imgArray[i].url = res.data.url; //
callback(res.data.url);
_this.imgArray[i].url = res?.data?.url || ''; //
callback(res?.data?.url || '');
} else {
_this.imgArray[i].result = false;
_this.tipObj.prompt = "上传失败,请检查!";
@ -947,4 +1054,12 @@ export default {
transform: translateX(0%);
opacity: 1;
}
/* 隐藏canvas仅用于后台处理 */
.cropper-canvas {
position: absolute;
left: -999999px;
top: -999999px;
z-index: -1;
}
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB