-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEAN128-4php.php
executable file
·204 lines (170 loc) · 6.9 KB
/
EAN128-4php.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
/*
This file is part of EAN128-4php.
EAN128-4php is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
EAN128-4php is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with EAN128-4php. If not, see <http://www.gnu.org/licenses/>.
Copyright 2012 Marc Carné
*/
/* ******************************************************************** */
/* COLORS */
/* ******************************************************************** */
$bar_color=Array(0,0,0);
$bg_color=Array(255,255,255);
$text_color=Array(0,0,0);
/* ******************************************************************** */
/* FONT */
/* ******************************************************************** */
$font_loc=dirname(__FILE__)."/"."FreeSans.ttf";
/* ******************************************************************** */
/* ENCODING MAPS */
/* ******************************************************************** */
include_once('encodings/Code128C_encoding.php');
class EAN1284php {
public function __construct() {
}
public function barcode_outputfile($image, $filename, $mode) {
/* output the image */
$mode = strtolower($mode);
if ($mode=='jpg' || $mode=='jpeg'){
$filename .= '.jpg';
imagejpeg($image,$filename);
} else if ($mode=='gif'){
$filename .= '.gif';
imagegif($image, $filename);
} else {
$filename .= '.png';
imagepng($image, $filename);
}
}
public function barcode_outimage($text, $bars, $scale = 1, $mode = "png",$total_y = 0, $space = '', $filename) {
/* we're going to use these globals */
global $bar_color, $bg_color, $text_color;
global $font_loc;
/* set defaults if not specified */
if ($scale<1) {
$scale=2;
}
$total_y=(int)$total_y;
if ($total_y<1) {
$total_y=(int)$scale * 60;
}
if (!$space) {
$space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale);
}
/* count total width based on the number of bars we need to paint */
$xpos=0;
for ($i=0, $len = strlen($bars);$i<$len;$i++){
$xpos+=1*$scale;
$width=true;
}
/* allocate the image */
$total_x = $xpos + $space['right'] + $space['right'];
$xpos=$space['left'];
if (!function_exists('imagecreate')){
// GD is not installed or enabled
print "You don't have the gd2 extension enabled\n";
return '';
}
$im=imagecreate($total_x, $total_y);
/* create image stuff */
$col_bg=imagecolorallocate($im,$bg_color[0],$bg_color[1],$bg_color[2]);
$col_bar=imagecolorallocate($im,$bar_color[0],$bar_color[1],$bar_color[2]);
$col_text=imagecolorallocate($im,$text_color[0],$text_color[1],$text_color[2]);
$height=round($total_y-$space['bottom']);
/* paint the bars */
for ($i=0, $len = strlen($bars);$i<$len;$i++){
$val=strtolower($bars[$i]);
$h=$height;
if ($val == "1") {
imagefilledrectangle($im, $xpos, $space['top'], $xpos, $h, $col_bar);
}
$xpos+=1*$scale;
}
/* write out the text */
$fontsize=$scale * 8;
$fontheight=$total_y-($fontsize/2.5)+2;
@imagettftext($im, $fontsize, 0, ($total_x / 2)-(strlen($text)*$fontsize/2.68), $fontheight, $col_text, $font_loc, $text);
if ($filename != "") {
$this->barcode_outputfile($im, $filename, $mode);
}
return $im;
}
public function create($barcode, $filename = "") {
$barcode_data = "";
/* Code128C character matching */
$encoding = new Code128cEncoding();
$code128c_codes = $encoding->getCodeMap();
try {
$arr_barcode = str_split($barcode, 2);
$checksum = (int) $code128c_codes['START_DATA'];
// Get barcode data
$i = 1;
foreach ($arr_barcode as $pair) {
$i++;
$checksum += (int) $pair * $i;
$trans_pair = $code128c_codes[$pair];
if ($trans_pair != '') {
$barcode_data .= $trans_pair;
} else {
throw new Exception('Incorrect barcode format.');
}
}
$checksum += (int) $code128c_codes['FNC1_DATA'] * 1;
$checksum = $checksum % 103;
$code_keys = array_keys($code128c_codes);
$barcode_data .= $code128c_codes[$code_keys[$checksum]];
// Buid final barcode
$final_barcode = $code128c_codes['START'] . $code128c_codes['FNC1'] . $barcode_data . $code128c_codes['STOP'] . $code128c_codes['TERMINATE'];
// Draw
return $this->barcode_outimage($barcode, $final_barcode, 1, 'PNG', 0, array('bottom' => 15, 'top' => 5, 'left' => 15, 'right' => 15), $filename);
} catch (Exception $e) {
print $e;
}
return null;
}
public function createImageBuffer($barcode) {
// Set content type
header('Content-Type: image/png');
header('Content-Disposition: Attachment;filename=image.png');
//header("Content-Length: " . strlen($im));
// Create the barcode
$im = $this->create($barcode);
imagepng($im);
imagedestroy($im);
}
public function printImageBuffer($barcode) {
// Set response headers to return an image
header('Content-type: image/png');
// Create the barcode
$im = $this->create($barcode);
imagepng($im);
imagedestroy($im);
}
public function createImageFile($barcode, $filename) {
if ($filename != '') {
$this->create($barcode, $filename);
} else {
print 'Filename must be specified. If you need a buffered image use createImageBuffer instead.';
}
}
}
if (isset($_GET['barcode'])) {
$ean128 = new EAN1284php();
$type = isset($_GET['type']) ? $_GET['type'] : 'default';
$barcode = $_GET['barcode'];
if ($barcode != '') {
if ($type == 'attach') {
$ean128->createImageBuffer($barcode);
} else {
$ean128->printImageBuffer($barcode);
}
}
}