captcha-canvas API Documentation - v3.3.4
    Preparing search index...

    Function createCaptchaSync

    • Creates a CAPTCHA image synchronously without async/await requirements.

      This function provides the same functionality as createCaptcha() but operates synchronously, making it suitable for use cases where async operations are not desired or possible. Note that background images must be pre-loaded using resolveImage().

      Parameters

      • width: number

        Width of the CAPTCHA image in pixels

      • height: number

        Height of the CAPTCHA image in pixels

      • option: CreateCaptchaOptions = {}

        Optional configuration for customizing CAPTCHA appearance and security

      Returns CaptchaValueSync

      Object containing the image buffer and solution text

      import { createCaptchaSync } from 'captcha-canvas';
      import fs from 'fs';

      const { image, text } = createCaptchaSync(300, 100);
      fs.writeFileSync('captcha.png', image);
      console.log('Solution:', text);
      const { image, text } = createCaptchaSync(400, 150, {
      captcha: {
      text: 'VERIFY',
      size: 55,
      rotate: 20,
      colors: ['#e74c3c', '#f39c12']
      },
      trace: { size: 3, opacity: 0.7 },
      decoy: { total: 30, opacity: 0.25 }
      });
      import { resolveImage } from 'captcha-canvas';

      // Pre-load background image
      const background = await resolveImage('./texture.png');

      // Generate synchronously
      const { image, text } = createCaptchaSync(300, 100, {
      background,
      captcha: { characters: 6, opacity: 0.85 }
      });
      function generateCaptchaForForm() {
      const { image, text } = createCaptchaSync(250, 80, {
      captcha: { characters: 5, size: 35 }
      });

      return {
      imageBuffer: image,
      solution: text,
      timestamp: Date.now()
      };
      }
      function generateMultipleCaptchas(count: number) {
      const captchas = [];

      for (let i = 0; i < count; i++) {
      const { image, text } = createCaptchaSync(300, 100);
      captchas.push({ image, text, id: i });
      }

      return captchas;
      }