-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoo.php
97 lines (71 loc) · 2.17 KB
/
moo.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
<?php
require_once('./phpqrcode/qrlib.php');
$script_name = $argv[0];
$options = getopt('hlg', array('help','list','generate'));
foreach ($options as $optkey => $optval) {
switch ($optkey) {
case 'h' :
case 'help' :
help();
exit(0);
break;
case 'g' :
if(count($argv) != 5){
echo "Incorrect syntax. \n";
help();
exit(0);
}
generate($argv[2], $argv[3], $argv[4]);
exit(0);
break;
default:
echo "Choose a valid option. \n";
help();
exit(0);
}
}
// Show help and exit if no options
if(count($options) <= 0){
help();
exit(0);
}
function help() {
global $script_name;
echo <<< HELP
Usage: {$script_name}
[-h|--help]
[-g|--generate <base_url> <count> <ouput_dir>]
Options:
-h --help Display this help message
-g --generate Generate QR codes as 283 x 283 pixel 300dpi pngs
HELP;
}
function generate($prefix_url, $count, $output_dir){
// public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false) ^M
for($i = 1; $count >= $i; $i++){
$id = generate_id();
$filename = $output_dir.'/'.$id.'.png';
qrcode::png('http://'.$prefix_url.'/'.$id, $filename, 'h', 4, 2);
$thumb = imagecreatetruecolor(283, 283);
$source = imagecreatefrompng($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, 283, 283, 132, 132);
// Write the qr url onto the code
$textcolor = imagecolorallocate($thumb, 158,158,158);
imagestring($thumb, 5, 63, 266, $prefix_url.'/'.$id, $textcolor);
// Output
imagepng($thumb, $filename);
}
}
function generate_id(){
static $generated = array();
while(true){
$id = uniqid();
$id = substr($id, 7, 6);
if(!in_array($id, $generated)){
$generated[] = $id;
break;
}
}
return $id;
}