微信小程序图片添加水印

页面布局

  <canvas class='canvas' style="width:{{canvasWidth}}px;height:{{canvasHeight}}px;top:{{canvasHeight*2}}px;" canvas-id="firstCanvas"></canvas>

css

.canvas{
    position: fixed;
    top: 200%;
    left: 0;
    /* background: red;*/
    width: 100%;
    height: 100%; 
}

js

      var that = this;
            wx.chooseImage({
                count: 9, // 默认9
                sourceType: ['camera'],
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
                success: function(res) {
                    // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
                    var arr = that.data.fileList
                    res.tempFilePaths.forEach(function(item) {
                        arr.push(item)
                    })
                    that.setData({
                        fileList: arr
                    })

                    //获取图片详细信息
                    wx.getImageInfo({
                        src: res.tempFilePaths[0],
                        success: (ress) => {

                            let date = util.formatTime(new Date());

                            let ctx = wx.createCanvasContext('firstCanvas')

                            that.setData({
                                canvasHeight: ress.height,
                                canvasWidth: ress.width
                            })

                            //将图片src放到cancas内,宽高为图片大小
                            ctx.drawImage(res.tempFilePaths[0], 0, 0, ress.width, ress.height)
                            //将声明的时间放入canvas
                            ctx.setFontSize(30) //注意:设置文字大小必须放在填充文字之前,否则不生效
                            ctx.setFillStyle('blue')
                            ctx.fillText(date, 0, 30)
                            ctx.strokeText(date, 0, 30)

                            ctx.draw(false, function() {
                                wx.canvasToTempFilePath({
                                    canvasId: 'firstCanvas',
                                    success: (res) => {
                                        wx.uploadFile({
                                            url: app.globalData.link + '/attach/upload',
                                            filePath: res.tempFilePath,
                                            name: 'file',
                                            formData: {

                                            },
                                            header: {
                                                "Content-Type": "multipart/form-data"
                                            },
                                            success: function(res) {
                                                res.data = JSON.parse(res.data)
                                                that.data.fileListIds.push(res.data.files[0].id)
                                            },
                                            fail: function(res) {},
                                            complete: function(res) {}
                                        })

                                    },
                                    fail: (e) => {
                                        console.log(e)
                                    }
                                })
                            })
                        }
                    })



                }
            })

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值