转化函数
// 转base64
getBase64(url, callback, outputFormat) {
var canvas = document.createElement('canvas')
var ctx = canvas.getContext("2d")
var img = new Image()
img.crossOrigin = 'Anonymous'
img.src = url
img.onload = function () {
canvas.height = img. height
canvas.width = img. width
ctx.drawImage(img, 0, 0, img.width, img.height)
var dataURL = canvas.toDataURL(outputFormat || 'image/png')
callback.call(this, dataURL)
canvas = null
}
}
使用方法
let newUrl = ""
const that = this
const url = "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=%E5%A4%B4%E5%83%8F&step_word=&hs=0&pn=66&spn=0&di=28410&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=2939425441%2C2024243876&os=3267401331%2C36694078&simid=3420651174%2C339152608&adpicid=0&lpn=0&ln=30&fr=&fmq=1461834053046_R&fm=&ic=0&s=0&hd=undefined&latest=undefined©right=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=head&bdtype=11&oriquery=&objurl=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202010%2F05%2F20201005094406_1051a.thumb.400_0.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B17tpwg2_z%26e3Bv54AzdH3Fwsk74AzdH3F%3Ft1%3D8anmcllc0&gsm=43&rpstart=0&rpnum=0&islist=&querylist=&force=undefined"
getBase64(url, function(base64Img) {
that.newUrl = base64Img
})