Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
3
 
4
/**
5
 * Image_Barcode_upca class
6
 *
7
 * Renders UPC-A 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     Jeffrey K. Brown <jkb@darkfantastic.net>
20
 * @author     Didier Fournout <didier.fournout@nyc.fr>
21
 * @copyright  2005 The PHP Group
22
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
23
 * @version    CVS: $Id: upca.php 304741 2010-10-25 09:14:17Z clockwerx $
24
 * @link       http://pear.php.net/package/Image_Barcode
25
 */
26
 
27
require_once 'Image/Barcode.php';
28
 
29
/**
30
 * Image_Barcode_upca class
31
 *
32
 * Package which provides a method to create UPC-A barcode using GD library.
33
 *
34
 * Slightly Modified ean13.php to get upca.php I needed a way to print
35
 * UPC-A bar codes on a PHP page.  The Image_Barcode class seemed like
36
 * the best way to do it, so I modified ean13 to print in the UPC-A format.
37
 * Checked the bar code tables against some documentation below (no errors)
38
 * and validated the changes with my trusty cue-cat.
39
 * http://www.indiana.edu/~atmat/units/barcodes/bar_t4.htm
40
 *
41
 * @category   Image
42
 * @package    Image_Barcode
43
 * @author     Jeffrey K. Brown <jkb@darkfantastic.net>
44
 * @author     Didier Fournout <didier.fournout@nyc.fr>
45
 * @copyright  2005 The PHP Group
46
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
47
 * @version    Release: @package_version@
48
 * @link       http://pear.php.net/package/Image_Barcode
49
 */
50
class Image_Barcode_upca extends Image_Barcode
51
{
52
    /**
53
     * Barcode type
54
     * @var string
55
     */
56
    var $_type = 'upca';
57
 
58
    /**
59
     * Barcode height
60
     *
61
     * @var integer
62
     */
63
    var $_barcodeheight = 50;
64
 
65
    /**
66
     * Font use to display text
67
     *
68
     * @var integer
69
     */
70
    var $_font = 2;  // gd internal small font
71
 
72
    /**
73
     * Bar width
74
     *
75
     * @var integer
76
     */
77
    var $_barwidth = 1;
78
 
79
 
80
    /**
81
     * Number set
82
     * @var array
83
     */
84
    var $_number_set = array(
85
           '0' => array(
86
                    'L' => array(0,0,0,1,1,0,1),
87
                    'R' => array(1,1,1,0,0,1,0)
88
                        ),
89
           '1' => array(
90
                    'L' => array(0,0,1,1,0,0,1),
91
                    'R' => array(1,1,0,0,1,1,0)
92
                        ),
93
           '2' => array(
94
                    'L' => array(0,0,1,0,0,1,1),
95
                    'R' => array(1,1,0,1,1,0,0)
96
                        ),
97
           '3' => array(
98
                    'L' => array(0,1,1,1,1,0,1),
99
                    'R' => array(1,0,0,0,0,1,0)
100
                        ),
101
           '4' => array(
102
                    'L' => array(0,1,0,0,0,1,1),
103
                    'R' => array(1,0,1,1,1,0,0)
104
                        ),
105
           '5' => array(
106
                    'L' => array(0,1,1,0,0,0,1),
107
                    'R' => array(1,0,0,1,1,1,0)
108
                        ),
109
           '6' => array(
110
                    'L' => array(0,1,0,1,1,1,1),
111
                    'R' => array(1,0,1,0,0,0,0)
112
                        ),
113
           '7' => array(
114
                    'L' => array(0,1,1,1,0,1,1),
115
                    'R' => array(1,0,0,0,1,0,0)
116
                        ),
117
           '8' => array(
118
                    'L' => array(0,1,1,0,1,1,1),
119
                    'R' => array(1,0,0,1,0,0,0)
120
                        ),
121
           '9' => array(
122
                    'L' => array(0,0,0,1,0,1,1),
123
                    'R' => array(1,1,1,0,1,0,0)
124
                        )
125
        );
126
 
127
    /**
128
     * Draws a UPC-A image barcode
129
     *
130
     * @param   string $text     A text that should be in the image barcode
131
     * @param   string $imgtype  The image type that will be generated
132
     *
133
     * @return  image            The corresponding Interleaved 2 of 5 image barcode
134
     *
135
     * @access  public
136
     *
137
     * @author  Jeffrey K. Brown <jkb@darkfantastic.net>
138
     * @author  Didier Fournout <didier.fournout@nyc.fr>
139
     *
140
     */
141
    function &draw($text, $imgtype = 'png')
142
    {
143
        $error = false;
144
        if ((is_numeric($text)==false) || (strlen($text)!=12)) {
145
            $barcodewidth= (12 * 7 * $this->_barwidth) + 3 + 5 + 3 + 2 * (imagefontwidth($this->_font)+1);
146
            $error = true;
147
        } else {
148
            // Calculate the barcode width
149
            $barcodewidth = (strlen($text)) * (7 * $this->_barwidth)
150
                + 3 // left
151
                + 5 // center
152
                + 3 // right
153
                + imagefontwidth($this->_font)+1
154
                + imagefontwidth($this->_font)+1   // check digit's padding
155
                ;
156
        }
157
 
158
        $barcodelongheight = (int) (imagefontheight($this->_font)/2)+$this->_barcodeheight;
159
 
160
        // Create the image
161
        $img = ImageCreate($barcodewidth, $barcodelongheight+ imagefontheight($this->_font)+1);
162
 
163
        // Alocate the black and white colors
164
        $black = ImageColorAllocate($img, 0, 0, 0);
165
        $white = ImageColorAllocate($img, 255, 255, 255);
166
 
167
        // Fill image with white color
168
        imagefill($img, 0, 0, $white);
169
 
170
        if ($error) {
171
            $imgerror = ImageCreate($barcodewidth, $barcodelongheight+imagefontheight($this->_font)+1);
172
            $red      = ImageColorAllocate($imgerror, 255, 0, 0);
173
            $black    = ImageColorAllocate($imgerror, 0, 0, 0);
174
            imagefill($imgerror, 0, 0, $red);
175
 
176
            imagestring(
177
                $imgerror,
178
                $this->_font,
179
                $barcodewidth / 2 - (10/2 * imagefontwidth($this->_font)),
180
                $this->_barcodeheight / 2,
181
                'Code Error',
182
                $black
183
            );
184
        }
185
 
186
        // get the first digit which is the key for creating the first 6 bars
187
        $key = substr($text,0,1);
188
 
189
        // Initiate x position
190
        $xpos = 0;
191
 
192
        // print first digit
193
        imagestring($img, $this->_font, $xpos, $this->_barcodeheight, $key, $black);
194
        $xpos= imagefontwidth($this->_font) + 1;
195
 
196
 
197
 
198
        // Draws the left guard pattern (bar-space-bar)
199
        // bar
200
        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
201
        $xpos += $this->_barwidth;
202
        // space
203
        $xpos += $this->_barwidth;
204
        // bar
205
        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
206
        $xpos += $this->_barwidth;
207
 
208
 
209
        foreach ($this->_number_set[$key]['L'] as $bar) {
210
            if ($bar) {
211
                imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
212
            }
213
            $xpos += $this->_barwidth;
214
        }
215
 
216
 
217
 
218
        // Draw left $text contents
219
        for ($idx = 1; $idx < 6; $idx ++) {
220
            $value=substr($text,$idx,1);
221
            imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black);
222
 
223
            foreach ($this->_number_set[$value]['L'] as $bar) {
224
                if ($bar) {
225
                    imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black);
226
                }
227
                $xpos += $this->_barwidth;
228
            }
229
        }
230
 
231
 
232
        // Draws the center pattern (space-bar-space-bar-space)
233
        // space
234
        $xpos += $this->_barwidth;
235
        // bar
236
        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
237
        $xpos += $this->_barwidth;
238
        // space
239
        $xpos += $this->_barwidth;
240
        // bar
241
        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
242
        $xpos += $this->_barwidth;
243
        // space
244
        $xpos += $this->_barwidth;
245
 
246
 
247
        // Draw right $text contents
248
        for ($idx = 6; $idx < 11; $idx ++) {
249
            $value=substr($text,$idx,1);
250
            imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black);
251
            foreach ($this->_number_set[$value]['R'] as $bar) {
252
                if ($bar) {
253
                    imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black);
254
                }
255
                $xpos += $this->_barwidth;
256
            }
257
        }
258
 
259
 
260
 
261
        $value = substr($text,11,1);
262
        foreach ($this->_number_set[$value]['R'] as $bar) {
263
            if ($bar) {
264
                imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
265
            }
266
            $xpos += $this->_barwidth;
267
        }
268
 
269
 
270
 
271
        // Draws the right guard pattern (bar-space-bar)
272
        // bar
273
        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
274
        $xpos += $this->_barwidth;
275
        // space
276
        $xpos += $this->_barwidth;
277
        // bar
278
        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
279
        $xpos += $this->_barwidth;
280
 
281
 
282
        // Print Check Digit
283
        imagestring($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black);
284
 
285
        if ($error) {
286
            return $imgerror;
287
        } else {
288
            return $img;
289
        }
290
    } // function create
291
 
292
} // class
293
?>