看看效果
实现步骤:
1.选择图片
chooseImage(e) {
uni.chooseImage({
count: 1,
success: (res) => {
let index = res.tempFilePaths[0].lastIndexOf(".");
let imgUrl = res.tempFilePaths[0].substr(index + 1);
if (imgUrl != "png" && imgUrl != "jpg" && imgUrl != "jpeg") {
uni.showToast({
title: '请上传jpg、jpeg、png类型的图片',
icon: 'none'
});
return
}
if (res.tempFiles[0].size / 1024 < 1024 * 1024 * 20) {
this.originImg = res.tempFilePaths[0]
this.imageSrc = res.tempFilePaths[0]
this.loadImage();
} else {
uni.showToast({
title: '图片大小不能超过20M,当前大小' + (res.tempFiles[0].size / 1024).toFixed(
2) + 'KB',
icon: 'none'
})
}
},
});
},
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
图片展示区域计算
loadImage() {
uni.showLoading({
title: "图片加载中...",
});
uni.getImageInfo({
src: this.imageSrc,
success:(res) => {
let imgH = res.height;
let imgW = res.width;
IMG_RATIO = imgW / imgH;
if (IMG_RATIO < 1 && (SCREEN_HEIGHT - 133) * IMG_RATIO < SCREEN_WIDTH - 10) {
IMG_REAL_W = (SCREEN_HEIGHT - 133) * IMG_RATIO;
IMG_REAL_H = SCREEN_HEIGHT - 133;
} else {
IMG_REAL_W = SCREEN_WIDTH - 10;
IMG_REAL_H = IMG_REAL_W / IMG_RATIO;
}
this.cropperW=IMG_REAL_W,
this.cropperH=IMG_REAL_H,
this.cropperL=Math.ceil((SCREEN_WIDTH - IMG_REAL_W) / 2),
this.cropperT=uni.getStorageSync("navHeight"),
this.imageW=IMG_REAL_W,
this.imageH=IMG_REAL_H,
this.isShowImg=true,
uni.hideLoading();
this.finish()
},
});
},
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
图片水印canvas绘制
finish() {
uni.showLoading({
title: "图片生成中...",
});
const ctx = uni.createCanvasContext("myCanvas", this);
ctx.clearRect(0, 0, IMG_REAL_W, IMG_REAL_H);
ctx.drawImage(this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);
ctx.save();
ctx.beginPath();
if (['alone'].includes(this.changeMode)) {
ctx.beginPath()
ctx.setFontSize(this.changeSize)
ctx.setFillStyle(this.changeColor)
ctx.fillText(this.changeText, this.imageW - this.changeSize * this.changeText.length - 10, this.imageH - this.changeSize)
}
if (['level'].includes(this.changeMode)) {
for (let j = 1; j < 12; j++) {
ctx.beginPath()
ctx.setFontSize(this.changeSize)
ctx.setFillStyle(this.changeColor)
ctx.fillText(this.changeText, 0, 50 * j)
for (let i = 1; i < 12; i++) {
ctx.beginPath()
ctx.setFontSize(this.changeSize)
ctx.setFillStyle(this.changeColor)
ctx.fillText(this.changeText, (15 + (this.changeSize - 1) * this.changeText.length) * i, 50 * j)
}
}
}
if (["incline"].includes(this.changeMode)) {
ctx.font = this.changeSize + "px serif";
ctx.fillText(this.changeText, -1000, -1000)
const textWidth = ctx.measureText(this.changeText + "").width
const _textWidth = textWidth * 1.5
const _textHeight = 40
const xSize = Math.floor(this.imageW / _textWidth + 1)
const ySize = Math.floor(this.imageH / _textHeight + 1)
for (var x = 0; x < xSize * 2; x++) {
for (var y = 0; y < ySize * 2; y++) {
this.drawText(ctx, (this.imageW), (this.imageH), -this.imageW / 2 + ((x * _textWidth)), -this.imageH / 2 + ((y * _textHeight)))
}
}
}
ctx.draw(true, () => {
var canvasW = IMG_REAL_W;
var canvasH = IMG_REAL_H;
var canvasL = 0;
var canvasT = 0;
uni.canvasToTempFilePath({
x: canvasL,
y: canvasT,
width: canvasW,
height: canvasH,
quality: +this.quality,
fileType: this.fileType,
canvasId: "myCanvas",
success: (res) => {
uni.hideLoading();
this.waterImgSrc = res.tempFilePath
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: "图片截取失败!",
icon: "none",
});
},
},
this
);
});
},
drawText(ctx, imgWidth, imgHeight, x, y) {
var text = this.changeText;
ctx.save();
ctx.translate(imgWidth / 2, imgHeight / 2)
ctx.rotate(-Math.PI / 6);
ctx.translate(-imgWidth / 2, -imgHeight / 2)
ctx.font = this.changeSize + "px serif";
ctx.fillStyle = this.changeColor;
ctx.fillText(text, x, y);
ctx.restore();
},
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
图片保存逻辑
saveImg() {
const path = this.waterImgSrc
uni.showShareImageMenu({
path,
success: (res)=>{
console.log(res)
wx.showToast({
title: '生成成功!',
icon: 'success',
duration: 2000
})
},
fail: (err)=> {
console.log(err)
wx.showToast({
title: '生成失败!',
icon: 'none',
duration: 2000
})
}
})
},
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
感觉有用的话,可以打赏一把么?一毛不嫌少,十块不嫌多
更多详细代码请关注公众号索取(备注:公众号):