-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
205 lines (186 loc) · 7.34 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
define('O_FORMAT_PNG', 'PNG');
define('O_FORMAT_SVG', 'SVG');
// include 2D barcode class (search for installation path)
function makepath() {
$array = func_get_args();
return implode(DIRECTORY_SEPARATOR, $array);
}
function check_for_gd() {
if (extension_loaded('gd') && function_exists('gd_info')) {
// echo "PHP GD library is enabled in your system.";
return true;
} else {
echo "PHP GD library is not enabled on your system.";
die();
}
}
require_once( makepath(dirname(__FILE__), 'tcpdf_barcodes_1d.php'));
require_once( makepath(dirname(__FILE__), 'tcpdf_barcodes_2d.php') );
require_once( makepath(dirname(__FILE__), 'tools', 'dir.php') );
// constants
if (array_key_exists('APPLICATION_ID', $_SERVER)) {
# Google cloud!
$tmp_dir = 'gs://com-larcity-static/temp/';
} else {
$tmp_dir = makepath(dirname(__FILE__), 'temp') . DIRECTORY_SEPARATOR;
}
define('TMP_DIR', $tmp_dir);
// defaults to FANCYQR
$requested_code_type = empty($_GET['type']) ? 'FANCYQR' : strtoupper(trim(filter_var($_GET['type'], FILTER_SANITIZE_STRING)));
$data = [];
$text_data = '';
function set($key, $value) {
$data[$key] = $value;
}
function json($key) {
return empty($data[$key]) ? null : $data[$key];
}
function respond($data, $contentType = 'text/plain') {
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
$http_response_code = empty($data['status']) ? 200 : intval($data['status']);
$status_message = empty($data['status_msg']) ? 'OK' : $data['status_msg'];
header($protocol . ' ' . $http_response_code . ' ' . $status_message);
header("Content-type: {$contentType}");
if (preg_match("/image\/*", $contentType)) {
// do not print anything - handled by library classes
echo $data;
} else {
switch ($contentType) {
case 'text/plain':
case 'text/csv':
echo $data;
break;
default:
echo json_encode($data);
break;
}
}
}
try {
// size parameters
$size = empty($_GET['size']) ? 5 : filter_var($_GET['size'], FILTER_SANITIZE_NUMBER_INT);
/*
$w = empty($_GET['w']) ? 5 : filter_var($_GET['w'], FILTER_SANITIZE_NUMBER_INT);
$h = empty($_GET['h']) ? 2 : filter_var($_GET['h'], FILTER_SANITIZE_NUMBER_INT);
*/
if (!empty($_GET['rgb'])) {
$rgb_string = str_replace('|', ',', $_GET['rgb']);
if (!preg_match("/^\d+,\d+,\d+$/", $rgb_string)) {
$rgb_string = '0,0,0';
}
} else {
$rgb_string = '0,0,0';
}
$rgb_array = explode(',', $rgb_string);
$red = $rgb_array[0];
$green = $rgb_array[1];
$blue = $rgb_array[2];
$payload = empty($_GET['data']) ? 'https://larcity.com' : $_GET['data'];
// figure out format for output image
$format = empty($_GET['format']) ? O_FORMAT_PNG : strtoupper($_GET['format']);
if (!in_array(trim($format), [O_FORMAT_PNG, O_FORMAT_SVG])) {
$format = O_FORMAT_PNG;
}
// Check for GD library
check_for_gd();
switch ($requested_code_type) {
case 'PDF417':
$w = min(10, max(5, $size));
$h = 2 / 5 * $w;
$barcodeObj = new TCPDF2DBarcode($payload, $requested_code_type);
$barcodeObj->getBarcodePNG($w, $h, [$red, $green, $blue]);
break;
case 'DATAMATRIX':
$w = min(10, max(5, $size));
$h = $w;
// set the barcode content and type
$barcodeobj = new TCPDF2DBarcode($payload, $requested_code_type);
if ($format === O_FORMAT_SVG):
/** @TODO support figuring out color names or validating for them in GET `color` * */
$barcodeobj->getBarcodeSVG($w, $h, 'black');
else:
$barcodeobj->getBarcodePNG($w, $h, [$red, $green, $blue]);
endif;
break;
case 'QR':
/** Handle raw-looking QR * */
$w = min(10, max(5, $size));
$h = $w;
// set the barcode content and type
$barcodeobj = new TCPDF2DBarcode($payload, 'QRCODE,H');
if ($format === O_FORMAT_SVG):
/** @TODO support figuring out color names or validating for them in GET `color` * */
$barcodeobj->getBarcodeSVG($w, $h, 'black');
else:
$barcodeobj->getBarcodePNG($w, $h, [$red, $green, $blue]);
endif;
break;
case 'BARCODE':
case '2D':
/* Size is requirement
$w = min(10, max(5, $size));
$h = $w;
*/
$barcodeobj = new TCPDFBarcode($payload, 'C128');
if ($format === O_FORMAT_SVG):
// for now, force height
$h = 30;
/** @TODO support figuring out color names or validating for them in GET `color` * */
$barcodeobj->getBarcodeSVG(2, 30, 'black');
else:
$barcodeobj->getBarcodePNG(2, 30, [$red, $green, $blue]);
endif;
break;
case 'FANCYQR':
/** @TODO handle fancy QR code creation - output in PNG ONLY! */
$imagePadding = 12; // @IMPORTANT - make an even number
$std_wh = max([$w, $h]);
$w = $size;
$h = $size;
$barcodeObj = new TCPDF2DBarcode($payload, 'QRCODE,H');
$imgData = $barcodeObj->getBarcodePNGData($w, $h, [$red, $green, $blue]);
$imgSize = getimagesizefromstring($imgData);
// get png-8 image
$img = imagecreatetruecolor($imgSize[0] + $imagePadding, $imgSize[1] + $imagePadding);
imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255));
// write temp file
$temp_filename = TMP_DIR . date('YmdHis') . '__TEMP.PNG';
file_put_contents($temp_filename, $imgData);
$imgsrc = imagecreatefrompng($temp_filename);
// convert to png-24
$margin = -1 * ($imagePadding / 2);
imagecopy($img, $imgsrc, 0, 0, $margin, $margin, $imgSize[0] + $imagePadding, $imgSize[1] + $imagePadding);
// config
$matrixPointSize = min(max((int) $size, 1), 50);
// blur factor
$i = 50;
$i = min(20, $matrixPointSize);
$i = max(5, $i);
while ($i--)
imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($img, IMG_FILTER_CONTRAST, -100);
// set white to transparent
imagecolortransparent($img, imagecolorallocate($img, 255, 255, 255));
/** alpha => 0 - 127 from opaque to transparent * */
$alpha = 0;
imagefilter($img, IMG_FILTER_COLORIZE, $red, $green, $blue, $alpha);
$final_imgfile = TMP_DIR . date("Ymd_His_") . '__' . rand(1000, 2000) . '.png';
// show image
header('Content-Type: image/png');
/*
// save file
imagepng($img, $final_imgfile);
echo file_get_contents($final_imgfile);
*/
// do not save file
imagepng($img, NULL);
imagedestroy($img);
unlink($temp_filename);
break;
}
} catch (Exception $ex) {
set('status', $ex->getCode());
set('status_msg', $ex->getMessage());
respond($data);
}