itriton-captcha/index.js

97 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2021-10-08 13:57:37 +08:00
const sharp = require("sharp");
2021-11-10 22:51:00 +08:00
const path = require("path");
const axios = require("axios");
const url = path.join(__dirname, "./images/default.jpeg");
2021-10-08 13:57:37 +08:00
const opts = {
width: 270,
height: 144,
size: 30,
2021-11-10 22:51:00 +08:00
url,
2021-10-08 13:57:37 +08:00
};
const create = async function (options) {
options = Object.assign({}, opts, options);
const { width, height, size, url } = options;
2021-11-11 10:54:44 +08:00
2021-11-10 22:51:00 +08:00
let image = url;
if (isUrl(url)) {
const axiosData = await axios({ url, responseType: "arraybuffer" });
image = axiosData.data;
}
2021-11-11 10:54:44 +08:00
2021-11-10 22:51:00 +08:00
const baseImage = await sharp(image).resize(width, height).png().toBuffer();
2021-10-08 13:57:37 +08:00
const left = randomRangeNum(size, width - size);
const top = randomRangeNum(size, height - size);
const border = 1;
2021-11-10 22:51:00 +08:00
2021-10-08 13:57:37 +08:00
// Generate Jigsaw Image
const jigsawBaseImage = await sharp(baseImage)
.extract({ left: 0, top: top, width: size, height: size })
.extend({
top: border,
bottom: border,
left: border,
right: border,
background: { r: 255, g: 255, b: 255, alpha: 1 },
})
.png()
.toBuffer();
const jigsawImage = await sharp(jigsawBaseImage)
.extend({
top: top - border,
bottom: height - top - size - border,
background: { r: 0, g: 0, b: 0, alpha: 0 },
})
.png()
.toBuffer();
const jigsawWhiteImage = await sharp({
create: {
width: size + border,
height: size + border,
channels: 4,
background: { r: 255, g: 255, b: 255, alpha: 0.5 },
},
})
.png()
.toBuffer();
// Generate Background Image
const backgroundImage = await sharp(baseImage)
.composite([
{
input: jigsawWhiteImage,
left: left,
top: top,
},
])
.png()
.toBuffer();
return {
backgroundImage: `data:image/jpg;base64,${backgroundImage.toString(
"base64"
)}`,
jigsawImage: `data:image/jpg;base64,${jigsawImage.toString("base64")}`,
2021-10-08 15:02:30 +08:00
x: left,
2021-10-08 13:57:37 +08:00
};
};
2021-11-10 22:51:00 +08:00
function isUrl(url) {
if (/^http|https:\/\/.*/i.test(url)) {
return true;
}
return false;
}
2021-10-08 13:57:37 +08:00
function randomRangeNum(min, max) {
const range = max - min;
const random = Math.random();
return min + Math.round(random * range);
}
module.exports.create = create;