Skip to content

Instantly share code, notes, and snippets.

@sunfkny
Last active October 10, 2023 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunfkny/9855f30e0e9eaaef7b81d67e0c860150 to your computer and use it in GitHub Desktop.
Save sunfkny/9855f30e0e9eaaef7b81d67e0c860150 to your computer and use it in GitHub Desktop.
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