Skip to content

Commit 83176c0

Browse files
Max BaseMax Base
authored andcommitted
Create captcha php file to create a random image
1 parent 932c215 commit 83176c0

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

captcha.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
// Max Base
3+
// https://github.com/BaseMax/PHPNiceCaptcha
4+
session_start();
5+
define("BASE", __DIR__ . "/");
6+
require_once "_core.php";
7+
$debug=false;
8+
$font_path = "/var/www/html/matno3/2.ttf";
9+
// $font_path = "/usr/share/nginx/html/" . $_GET["font"]. ".ttf";
10+
// arial.ttf";
11+
// $debug=true; // Never not use this in public domain or place! IT'S YOUR RISK!
12+
13+
if(!isset($_GET["code"])) {
14+
exit();
15+
}
16+
$code=$_GET["code"];
17+
$count=$db->count("captcha", ["codes"=>$code]);
18+
if($count == 0) {
19+
exit();
20+
}
21+
$captcha=$db->select("captcha", ["codes"=>$code]);
22+
$width=640;
23+
$height=480;
24+
$codeLength=4;
25+
$chars="123456789";
26+
$lineInPart=2;
27+
$CircleInPart=3;
28+
$lineInAll=4;
29+
$CircleInAll=6;
30+
$code=$captcha["code"];
31+
$_SESSION["captcha"]=$code;
32+
if($debug == true && isset($_GET['code'])) {
33+
$code=$_GET['code'];
34+
}
35+
$image = imagecreatetruecolor($width, $height);
36+
$cHeight=$height;
37+
$cWidth=$width / $codeLength;
38+
imageantialias($image, true);
39+
$colors=[
40+
#16F292, rgb(22,242,146)
41+
//imagecolorallocate($image, 22,242,146),
42+
#b6177b, rgb(182,23,123)
43+
//imagecolorallocate($image, 182,23,123),
44+
#3a2b00, rgb(58,43,0)
45+
//imagecolorallocate($image, 58,43,0),
46+
#9f1435, rgb(159,20,53)
47+
//imagecolorallocate($image, 159,20,53),
48+
49+
randColor(),
50+
randColor(),
51+
randColor(),
52+
randColor(),
53+
];
54+
$white = imagecolorallocate($image, 255, 255, 255);
55+
$black = imagecolorallocate($image, 0, 0, 0);
56+
function randColor() {
57+
global $image;
58+
return imagecolorallocate($image, rand(0, 95), rand(0, 95), rand(0, 95));
59+
}
60+
for($i=0;$i<$codeLength;$i++) {
61+
$x1=$i * $cWidth;
62+
$y1=0;
63+
$x2=$x1 + $cWidth;
64+
$y2=$cHeight;
65+
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $colors[$i]);
66+
$text = $code[$i];
67+
imagettftext($image, 65, rand(0, 80), $x1 + rand(60, 90), rand(80, $height-30), randColor(), $font_path, $text);
68+
for($li=0;$li<$lineInPart;$li++) {
69+
// drawLine
70+
$textColor=$colors[$i];
71+
while($textColor == $colors[$i]) {
72+
$textColor=randColor();
73+
}
74+
imageline($image, $x1+rand(-7, 10), rand(10, $height-5), $x2-rand(5,10), rand(10, $height-5), $textColor);
75+
}
76+
for($ci=0;$ci<$CircleInPart;$ci++) {
77+
// drawCircle
78+
$color=imagecolorallocate($image, rand(0,190), rand(0,190), rand(0,190));
79+
$size=rand(7,10);
80+
imagefilledellipse($image, $x1+rand(-7, 10), rand(10, $height-5), $size, $size, randColor());
81+
}
82+
}
83+
for($li=0;$li<$lineInAll;$li++) {
84+
// drawLine
85+
imageline($image, rand(-7, $width), rand(10, $height-5), $x2-rand(5,10), rand(10, $height-5), randColor());
86+
}
87+
for($ci=0;$ci<$CircleInAll;$ci++) {
88+
// drawCircle
89+
$color=imagecolorallocate($image, rand(0,190), rand(0,190), rand(0,190));
90+
$size=rand(7,15);
91+
imagefilledellipse($image, rand(-7, $width-20), rand(10, $height-5), $size, $size, randColor());
92+
}
93+
header("Content-type: image/png");
94+
imagepng($image);

0 commit comments

Comments
 (0)