-
Notifications
You must be signed in to change notification settings - Fork 17
/
Barcode2.php
156 lines (138 loc) · 4.85 KB
/
Barcode2.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
<?php
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
/**
* Image_Barcode2 class
*
* Package to render barcodes
*
* PHP versions 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Image
* @package Image_Barcode2
* @author Marcelo Subtil Marcal <msmarcal@php.net>
* @copyright 2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Image_Barcode2
*/
require_once 'Image/Barcode2/Writer.php';
require_once 'Image/Barcode2/Driver.php';
require_once 'Image/Barcode2/Exception.php';
/**
* Image_Barcode2 class
*
* Package which provides a method to create barcode using GD library.
*
* @category Image
* @package Image_Barcode2
* @author Marcelo Subtil Marcal <msmarcal@php.net>
* @copyright 2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: @package_version@
* @link http://pear.php.net/package/Image_Barcode2
*/
class Image_Barcode2
{
/**
* Image type
*/
const IMAGE_PNG = 'png';
const IMAGE_GIF = 'gif';
const IMAGE_JPEG = 'jpg';
/**
* Barcode type
*/
const BARCODE_CODE39 = 'code39';
const BARCODE_INT25 = 'int25';
const BARCODE_EAN13 = 'ean13';
const BARCODE_UPCA = 'upca';
const BARCODE_UPCE = 'upce';
const BARCODE_CODE128 = 'code128';
const BARCODE_EAN8 = 'ean8';
const BARCODE_POSTNET = 'postnet';
/**
* Draws a image barcode
*
* @param string $text A text that should be in the image barcode
* @param string $type The barcode type. Supported types:
* code39 - Code 3 of 9
* int25 - 2 Interleaved 5
* ean13 - EAN 13
* upca - UPC-A
* upce - UPC-E
* code128
* ean8
* postnet
* @param string $imgtype The image type that will be generated
* (gif, jpg, png)
* @param boolean $bSendToBrowser if the image shall be outputted to the
* browser, or be returned.
* @param integer $height The image height
* @param integer $width The image width
*
* @return resource The corresponding gd image resource
*
* @throws Image_Barcode2_Exception
* @access public
*
* @author Marcelo Subtil Marcal <msmarcal@php.net>
* @since Image_Barcode2 0.3
*/
public static function draw($text,
$type = Image_Barcode2::BARCODE_INT25,
$imgtype = Image_Barcode2::IMAGE_PNG,
$bSendToBrowser = true,
$height = 60,
$width = 1
) {
//Make sure no bad files are included
if (!preg_match('/^[a-zA-Z0-9]+$/', $type)) {
throw new Image_Barcode2_Exception('Invalid barcode type ' . $type);
}
if (!include_once 'Image/Barcode2/Driver/' . ucfirst($type) . '.php') {
throw new Image_Barcode2_Exception($type . ' barcode is not supported');
}
$classname = 'Image_Barcode2_Driver_' . ucfirst($type);
$obj = new $classname(new Image_Barcode2_Writer());
if (!$obj instanceof Image_Barcode2_Driver) {
throw new Image_Barcode2_Exception(
"'$classname' does not implement Image_Barcode2_Driver"
);
}
if (!$obj instanceof Image_Barcode2_DualWidth) {
$obj->setBarcodeWidth($width);
}
if (!$obj instanceof Image_Barcode2_DualHeight) {
$obj->setBarcodeHeight($height);
}
$obj->setBarcode($text);
$obj->validate();
$img = $obj->draw();
if ($bSendToBrowser) {
// Send image to browser
switch ($imgtype) {
case self::IMAGE_GIF:
header('Content-type: image/gif');
imagegif($img);
imagedestroy($img);
break;
case self::IMAGE_JPEG:
header('Content-type: image/jpg');
imagejpeg($img);
imagedestroy($img);
break;
default:
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
break;
}
}
return $img;
}
}