Last active
October 10, 2023 04:43
-
-
Save sunfkny/9855f30e0e9eaaef7b81d67e0c860150 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getBingImageBase64() { | |
const response = await fetch("https://api.likepoems.com/img/bing/?type=JSON"); | |
const data = await response.json(); | |
const imgUrl = data.url.replace( | |
"https://jihulab.com/weblog/gallery02/-/raw/master/BingImage/", | |
"https://raw.githubusercontent.com/myseil/BingWallpaper/main/BingImage/" | |
// 也可以换 ghproxy 或者其他允许跨域的代理 | |
// "https://ghproxy.com/https://raw.githubusercontent.com/myseil/BingWallpaper/main/BingImage/" | |
); | |
return new Promise((resolve, reject) => { | |
const img = new Image(); | |
img.crossOrigin = "anonymous"; | |
img.onload = () => { | |
const canvas = document.createElement("canvas"); | |
canvas.width = img.width; | |
canvas.height = img.height; | |
const ctx = canvas.getContext("2d"); | |
ctx.drawImage(img, 0, 0); | |
const base64 = canvas.toDataURL("image/jpeg"); | |
resolve(base64); | |
}; | |
img.onerror = reject; | |
img.src = imgUrl; | |
}); | |
} | |
getBingImageBase64().then((base64) => { | |
console.log(base64); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment