Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const globalClient = require('../../../client.js');
- const svgCaptcha = require('svg-captcha');
- const sharp = require('sharp');
- class CaptchaGenerator {
- constructor() {
- this.options = {
- size: 6, // Length of captcha text
- noise: 8, // Number of noise lines
- color: true, // Colors enabled
- background: 'transparent',
- width: 300,
- height: 150,
- fontSize: 100,
- charPreset: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' // Characters to use
- };
- }
- /**
- * Generate a captcha with both text and image
- * @returns {Promise<{text: string, image: Buffer}>} Captcha text and PNG buffer
- */
- async generate() {
- try {
- // Generate SVG captcha
- const captcha = svgCaptcha.create(this.options);
- // Convert SVG to PNG using sharp
- const pngBuffer = await sharp(Buffer.from(captcha.data))
- .composite([{
- input: Buffer.from(`<svg>
- <rect width="100%" height="100%" fill="white"/>
- </svg>`),
- blend: 'dest-over'
- }])
- .png()
- .toBuffer();
- return {
- text: captcha.text, // The captcha solution
- imageBuffer: pngBuffer // PNG buffer that can be used to save or send the image
- };
- } catch (error) {
- throw new Error(`Failed to generate captcha: ${error.message}`);
- }
- }
- /**
- * Customize captcha generation options
- * @param {Object} newOptions - Options to override defaults
- */
- setOptions(newOptions) {
- this.options = {
- ...this.options,
- ...newOptions
- };
- }
- }
- module.exports = {
- name: 'captchaGenerator',
- priority: 0,
- initialize: async () => {
- const generator = new CaptchaGenerator();
- const namespace = globalClient.registerNamespace('captcha');
- namespace.generate = generator.generate.bind(generator);
- namespace.setOptions = generator.setOptions.bind(generator);
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement