-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.php
56 lines (43 loc) · 1.31 KB
/
image.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
<?php
use Sunlight\Captcha\Text3dCaptcha;
use Sunlight\Core;
use Sunlight\Extend;
use Sunlight\Image\ImageFormat;
use Sunlight\Image\ImageTransformer;
use Sunlight\Util\Request;
require __DIR__ . '/../../bootstrap.php';
Core::init(['content_type' => false]);
// fetch code
$captchaNumber = Request::get('n');
if (!empty($captchaNumber) && isset($_SESSION['captcha_code'][$captchaNumber])) {
[$captchaCode, $captchaDrawn] = $_SESSION['captcha_code'][$captchaNumber];
if ($captchaDrawn) {
http_response_code(403);
exit;
}
$_SESSION['captcha_code'][$captchaNumber][1] = true;
} else {
http_response_code(403);
exit;
}
// generate image
$captchaRenderer = new Text3dCaptcha();
$captchaRenderer->setLetterSpacing(2);
Extend::call('captcha.render', [
'renderer' => &$captchaRenderer,
'code' => &$captchaCode,
]);
$captcha = $captchaRenderer->draw($captchaCode);
$captchaResizeOptions = [
'w' => floor($captcha->width / 2),
'h' => floor($captcha->height / 2),
'mode' => 'fit',
];
Extend::call('captcha.render.resize', [
'captcha' => $captcha,
'options' => &$captchaResizeOptions,
]);
$captcha = ImageTransformer::resize($captcha, $captchaResizeOptions);
// output image
header('Content-Type: image/jpeg');
$captcha->output(ImageFormat::JPG, ['jpg_quality' => 50]);