Waifu-pics / waifu-api

https://waifu.pics Image sharing platform and API for anime images, entirely user curated (joke gone too far)
https://waifu.pics
ISC License
423 stars 35 forks source link

[Request] Allow use of the image data #55

Open anon25519 opened 2 years ago

anon25519 commented 2 years ago

It is currently impossible to use the image data due to CORS settings. To fix this, the server must be configured with the Access-Control-Allow-Origin: * header.

Sample code:

(async function f() {
  let img = new Image();
  img.crossOrigin = 'anonymous'; // allow non-authenticated downloading of the image cross-origin
  img.src = 'https://i.waifu.pics/YY~unmW.jpg'; // failed with error "CORS Missing Allow Origin"
  await img.decode();
  let bitmap = await createImageBitmap(img);
  let canvas = document.createElement('canvas');
  canvas.width = bitmap.width;
  canvas.height = bitmap.height;
  let ctx = canvas.getContext('2d');
  ctx.drawImage(bitmap, 0, 0);
  let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  console.log(imageData);
})();
JSH32 commented 2 years ago

Will be done soon during rewrite, I meant to do this before but it was being funky last time.