MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / generateImageCaptcha

Function generateImageCaptcha

pmcaptcha/pmcaptcha.ts:557–655  ·  view source on GitHub ↗
(
  digitOnly: boolean
)

Source from the content-addressed store, hash-verified

555 const CHARSET_MIXED = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
556
557 const charset = digitOnly ? CHARSET_DIGIT : CHARSET_MIXED;
558 const LENGTH = 5;
559
560 let answer = "";
561 for (let i = 0; i < LENGTH; i++) {
562 answer += charset[Math.floor(Math.random() * charset.length)];
563 }
564
565 const W = 240, H = 90;
566 const canvas = cv.createCanvas(W, H);
567 const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
568
569 const rnd = () => Math.random();
570 const rndI = (min: number, max: number) => Math.floor(rnd() * (max - min + 1)) + min;
571
572 for (let i = 0; i < 12; i++) {
573 ctx.fillStyle = `hsla(${rndI(0,360)},30%,${rndI(85,97)}%,0.9)`;
574 ctx.fillRect(rndI(0, W), rndI(0, H), rndI(20, 80), rndI(20, 60));
575 }
576
577 ctx.strokeStyle = `rgba(180,180,200,0.35)`;
578 ctx.lineWidth = 0.8;
579 for (let x = 0; x < W; x += rndI(18, 28)) {
580 ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, H); ctx.stroke();
581 }
582 for (let y = 0; y < H; y += rndI(18, 28)) {
583 ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(W, y); ctx.stroke();
584 }
585
586 for (let i = 0; i < 8; i++) {
587 ctx.beginPath();
588 ctx.moveTo(0, rnd() * H);
589 ctx.bezierCurveTo(W * 0.25, rnd() * H, W * 0.75, rnd() * H, W, rnd() * H);
590 ctx.strokeStyle = `hsla(${rndI(0,360)},55%,45%,${0.3 + rnd() * 0.3})`;
591 ctx.lineWidth = 1 + rnd();
592 ctx.stroke();
593 }
594
595 for (let i = 0; i < 180; i++) {
596 ctx.beginPath();
597 ctx.arc(rnd() * W, rnd() * H, 0.8 + rnd() * 1.5, 0, Math.PI * 2);
598 ctx.fillStyle = `hsla(${rndI(0,360)},50%,35%,${0.4 + rnd() * 0.4})`;
599 ctx.fill();
600 }
601
602 for (let i = 0; i < 8; i++) {
603 ctx.fillStyle = `hsla(${rndI(0,360)},60%,60%,0.15)`;
604 ctx.fillRect(rnd() * W, rnd() * H, rndI(8, 30), rndI(4, 16));
605 }
606
607 const STEP = (W - 24) / LENGTH;
608 for (let i = 0; i < LENGTH; i++) {
609 const ch = answer[i];
610 const x = 14 + i * STEP + STEP * 0.35;
611 const y = H / 2 + (rnd() - 0.5) * 18;
612 const rot = (rnd() - 0.5) * 0.65;
613 const sz = 32 + rndI(0, 10);
614

Callers 1

sendCaptchaFunction · 0.85

Calls 4

tryGetCanvasFunction · 0.85
rndIFunction · 0.85
rndFunction · 0.85
saveMethod · 0.80

Tested by

no test coverage detected