-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
75 lines (63 loc) · 2.3 KB
/
api.php
File metadata and controls
75 lines (63 loc) · 2.3 KB
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
67
68
69
70
71
72
73
74
75
<?php
/**
* qr. — API endpoint for server-side QR code generation
* GET params: data, level, size, fg, bg, dl
*/
header('Access-Control-Allow-Origin: *');
header('X-Content-Type-Options: nosniff');
include __DIR__ . '/qrlib.php';
$data = isset($_GET['data']) ? trim($_GET['data']) : '';
$level = isset($_GET['level']) && in_array($_GET['level'], ['L','M','Q','H'])
? $_GET['level'] : 'M';
$size = isset($_GET['size']) ? min(max((int)$_GET['size'], 1), 10) : 5;
$fg = isset($_GET['fg']) ? preg_replace('/[^0-9a-fA-F]/', '', $_GET['fg']) : '000000';
$bg = isset($_GET['bg']) ? preg_replace('/[^0-9a-fA-F]/', '', $_GET['bg']) : 'ffffff';
$dl = isset($_GET['dl']) && $_GET['dl'] === '1';
if (strlen($fg) !== 6) $fg = '000000';
if (strlen($bg) !== 6) $bg = 'ffffff';
if ($data === '') {
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(['error' => 'data param is required']);
exit;
}
// Ensure temp dir exists
$tempDir = __DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;
if (!is_dir($tempDir) && !@mkdir($tempDir, 0755, true)) {
http_response_code(500);
echo 'Cannot create temp directory';
exit;
}
// Generate PNG via library
$filename = $tempDir . 'api_' . md5($data . $level . $size) . '.png';
QRcode::png($data, $filename, $level, $size, 2);
if (!file_exists($filename)) {
http_response_code(500);
echo 'QR generation failed';
exit;
}
// Re-color using GD palette manipulation
$img = imagecreatefrompng($filename);
if (!$img) {
http_response_code(500);
echo 'Image load failed';
exit;
}
$fgR = hexdec(substr($fg, 0, 2));
$fgG = hexdec(substr($fg, 2, 2));
$fgB = hexdec(substr($fg, 4, 2));
$bgR = hexdec(substr($bg, 0, 2));
$bgG = hexdec(substr($bg, 2, 2));
$bgB = hexdec(substr($bg, 4, 2));
// The QR image is a 2-color palette image — swap palette entries directly
$blackIdx = imagecolorexact($img, 0, 0, 0);
$whiteIdx = imagecolorexact($img, 255, 255, 255);
if ($blackIdx !== -1) imagecolorset($img, $blackIdx, $fgR, $fgG, $fgB);
if ($whiteIdx !== -1) imagecolorset($img, $whiteIdx, $bgR, $bgG, $bgB);
if ($dl) {
header('Content-Disposition: attachment; filename="qr-code.png"');
}
header('Content-Type: image/png');
header('Cache-Control: public, max-age=3600');
imagepng($img);
imagedestroy($img);