代码拉取完成,页面将自动刷新
同步操作将从 智鲸开源/itriton-captcha 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
const sharp = require("sharp");
const path = require("path");
const axios = require("axios");
const url = path.join(__dirname, "./images/default.jpeg");
const opts = {
width: 270,
height: 144,
size: 30,
url,
};
const create = async function (options) {
options = Object.assign({}, opts, options);
const { width, height, size, url } = options;
let image = url;
if (isUrl(url)) {
const axiosData = await axios({ url, responseType: "arraybuffer" });
image = axiosData.data;
}
const baseImage = await sharp(image).resize(width, height).png().toBuffer();
const left = randomRangeNum(size, width - size);
const top = randomRangeNum(size, height - size);
const border = 1;
// 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")}`,
x: left,
};
};
function isUrl(url) {
if (/^http|https:\/\/.*/i.test(url)) {
return true;
}
return false;
}
function randomRangeNum(min, max) {
const range = max - min;
const random = Math.random();
return min + Math.round(random * range);
}
module.exports.create = create;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。