| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Image_Barcode class
|
|
|
6 |
*
|
|
|
7 |
* Package to render barcodes
|
|
|
8 |
*
|
|
|
9 |
* PHP versions 4
|
|
|
10 |
*
|
|
|
11 |
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
|
|
12 |
* that is available through the world-wide-web at the following URI:
|
|
|
13 |
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
|
|
14 |
* the PHP License and are unable to obtain it through the web, please
|
|
|
15 |
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
16 |
*
|
|
|
17 |
* @category Image
|
|
|
18 |
* @package Image_Barcode
|
|
|
19 |
* @author Marcelo Subtil Marcal <msmarcal@php.net>
|
|
|
20 |
* @copyright 2005 The PHP Group
|
|
|
21 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
22 |
* @version CVS: $Id: Barcode.php 304741 2010-10-25 09:14:17Z clockwerx $
|
|
|
23 |
* @link http://pear.php.net/package/Image_Barcode
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
require_once 'PEAR.php';
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Image_Barcode class
|
|
|
30 |
*
|
|
|
31 |
* Package which provides a method to create barcode using GD library.
|
|
|
32 |
*
|
|
|
33 |
* @category Image
|
|
|
34 |
* @package Image_Barcode
|
|
|
35 |
* @author Marcelo Subtil Marcal <msmarcal@php.net>
|
|
|
36 |
* @copyright 2005 The PHP Group
|
|
|
37 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
38 |
* @version Release: @package_version@
|
|
|
39 |
* @link http://pear.php.net/package/Image_Barcode
|
|
|
40 |
*/
|
|
|
41 |
class Image_Barcode extends PEAR
|
|
|
42 |
{
|
|
|
43 |
/**
|
|
|
44 |
* Draws a image barcode
|
|
|
45 |
*
|
|
|
46 |
* @param string $text A text that should be in the image barcode
|
|
|
47 |
* @param string $type The barcode type. Supported types:
|
|
|
48 |
* Code39 - Code 3 of 9
|
|
|
49 |
* int25 - 2 Interleaved 5
|
|
|
50 |
* ean13 - EAN 13
|
|
|
51 |
* upca - UPC-A
|
|
|
52 |
* @param string $imgtype The image type that will be generated
|
|
|
53 |
* @param boolean $bSendToBrowser if the image shall be outputted to the
|
|
|
54 |
* browser, or be returned.
|
|
|
55 |
*
|
|
|
56 |
* @return image The corresponding gd image object;
|
|
|
57 |
* PEAR_Error on failure
|
|
|
58 |
*
|
|
|
59 |
* @access public
|
|
|
60 |
*
|
|
|
61 |
* @author Marcelo Subtil Marcal <msmarcal@php.net>
|
|
|
62 |
* @since Image_Barcode 0.3
|
|
|
63 |
*/
|
|
|
64 |
function &draw($text, $type = 'int25', $imgtype = 'png', $bSendToBrowser = true)
|
|
|
65 |
{
|
|
|
66 |
//Make sure no bad files are included
|
|
|
67 |
if (!preg_match('/^[a-zA-Z0-9_-]+$/', $type)) {
|
|
|
68 |
return PEAR::raiseError('Invalid barcode type ' . $type);
|
|
|
69 |
}
|
|
|
70 |
if (!include_once('Image/Barcode/' . $type . '.php')) {
|
|
|
71 |
return PEAR::raiseError($type . ' barcode is not supported');
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$classname = 'Image_Barcode_' . $type;
|
|
|
75 |
|
|
|
76 |
if (!in_array('draw',get_class_methods($classname))) {
|
|
|
77 |
return PEAR::raiseError("Unable to find draw method in '$classname' class");
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
@$obj =& new $classname();
|
|
|
81 |
|
|
|
82 |
$img = &$obj->draw($text, $imgtype);
|
|
|
83 |
|
|
|
84 |
if (PEAR::isError($img)) {
|
|
|
85 |
return $img;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if ($bSendToBrowser) {
|
|
|
89 |
// Send image to browser
|
|
|
90 |
switch ($imgtype) {
|
|
|
91 |
case 'gif':
|
|
|
92 |
header('Content-type: image/gif');
|
|
|
93 |
imagegif($img);
|
|
|
94 |
imagedestroy($img);
|
|
|
95 |
break;
|
|
|
96 |
|
|
|
97 |
case 'jpg':
|
|
|
98 |
header('Content-type: image/jpg');
|
|
|
99 |
imagejpeg($img);
|
|
|
100 |
imagedestroy($img);
|
|
|
101 |
break;
|
|
|
102 |
|
|
|
103 |
default:
|
|
|
104 |
header('Content-type: image/png');
|
|
|
105 |
imagepng($img);
|
|
|
106 |
imagedestroy($img);
|
|
|
107 |
break;
|
|
|
108 |
}
|
|
|
109 |
} else {
|
|
|
110 |
return $img;
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
?>
|