forked from postman/itriton-captcha
✨ 支持传在线图片
This commit is contained in:
parent
7a9c1380d4
commit
b9b4228279
24
index.js
24
index.js
|
@ -1,22 +1,29 @@
|
|||
const sharp = require("sharp");
|
||||
const path = require("path")
|
||||
const url = path.join(__dirname, "./images/default.jpeg")
|
||||
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
|
||||
url,
|
||||
};
|
||||
|
||||
const create = async function (options) {
|
||||
options = Object.assign({}, opts, options);
|
||||
const { width, height, size, url } = options;
|
||||
const baseImage = await sharp(url).resize(width, height).png().toBuffer();
|
||||
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 })
|
||||
|
@ -71,6 +78,13 @@ const create = async function (options) {
|
|||
};
|
||||
};
|
||||
|
||||
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();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"name": "itriton-captcha",
|
||||
"name": "@itriton/captcha",
|
||||
"version": "0.3.2",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
|
@ -23,6 +23,14 @@
|
|||
"readable-stream": "^2.0.6"
|
||||
}
|
||||
},
|
||||
"axios": {
|
||||
"version": "0.24.0",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz",
|
||||
"integrity": "sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==",
|
||||
"requires": {
|
||||
"follow-redirects": "^1.14.4"
|
||||
}
|
||||
},
|
||||
"base64-js": {
|
||||
"version": "1.5.1",
|
||||
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
|
||||
|
@ -146,6 +154,11 @@
|
|||
"resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz",
|
||||
"integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg=="
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.14.5",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz",
|
||||
"integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA=="
|
||||
},
|
||||
"fs-constants": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz",
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
"author": "icjs-cc",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^0.24.0",
|
||||
"sharp": "^0.29.1"
|
||||
},
|
||||
"devDependencies": {}
|
||||
|
|
Loading…
Reference in New Issue