-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phimcap.php
66 lines (56 loc) · 1.69 KB
/
Phimcap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* phimcap - PHpIMageCAPtcha
* @see https://github.com/gueff/phimcap
*/
class Phimcap
{
/**
* @param string $sCaptchaText
* @param string $sAbsPathToFont
* @return bool
*/
public static function image (string $sCaptchaText = '', string $sAbsPathToFont = '/usr/share/fonts/truetype/freefont/FreeMono.ttf')
{
if (true === empty($sCaptchaText))
{
exit();
}
$iLength = strlen($sCaptchaText);
$iWidth = ($iLength * 30);
$iHeight = 50;
$oGdImage = imagecreatetruecolor($iWidth, $iHeight);
imagealphablending($oGdImage, true);
imagesavealpha($oGdImage, true);
$iBgColor = imagecolorallocatealpha($oGdImage, 255, 255, 255, 127);
imagefill($oGdImage, 0, 0, $iBgColor);
$iSize = 15;
$iColor = imagecolorallocate($oGdImage, 0, 0, 0);
for ($i = 0; $i < strlen($sCaptchaText); $i++)
{
$char = $sCaptchaText[$i];
imagettftext($oGdImage, $iSize, 0, 10 + $i * 30, 35, $iColor, $sAbsPathToFont, $char);
}
header("Content-Type: image/png");
imagepng($oGdImage);
imagedestroy($oGdImage);
exit();
}
/**
* @param int $iLentgh
* @return string
*/
public static function text(int $iLentgh = 5)
{
$iLentgh = abs($iLentgh);
($iLentgh < 5 || $iLentgh > 10) ? $iLentgh = 5 : false;
$sChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ1234567890";
$sText = "";
for ($i = 0; $i < $iLentgh; $i++)
{
$char = $sChar[rand(0, strlen($sChar) - 1)];
$sText.= $char;
}
return $sText;
}
}