1<?php
2/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
3
4/**
5 * Image_Barcode_ean8 class
6 *
7 * Renders EAN 8 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     Tobias Frost <tobi@coldtobi.de> ,
20 * 			   based on EAN13 code by 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:
24 * @link       http://pear.php.net/package/Image_Barcode
25 */
26
27require_once 'Image/Barcode.php';
28
29/**
30 * Image_Barcode_ean8 class
31 *
32 * Package which provides a method to create EAN 13 barcode using GD library.
33 *
34 * @category   Image
35 * @package    Image_Barcode
36 * @author     Tobias Frost <tobi@coldtobi.de> ,
37 * 			   based on EAN13 code by Didier Fournout <didier.fournout@nyc.fr>
38 * @copyright  2005 The PHP Group
39 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
40 * @version    Release: @package_version@
41 * @link       http://pear.php.net/package/Image_Barcode
42 */
43class Image_Barcode_ean8 extends Image_Barcode
44{
45    /**
46     * Barcode type
47     * @var string
48     */
49    var $_type = 'ean8';
50
51    /**
52     * Barcode height
53     *
54     * @var integer
55     */
56    var $_barcodeheight = 50;
57
58    /**
59     * Font use to display text
60     *
61     * @var integer
62     */
63    var $_font = 2;  // gd internal small font
64
65    /**
66     * Bar width
67     *
68     * @var integer
69     */
70    var $_barwidth = 1;
71
72
73    /**
74     * Number set
75     * @var array
76     */
77    var $_number_set = array(
78           '0' => array(
79                    'A' => array(0,0,0,1,1,0,1),
80                    'C' => array(1,1,1,0,0,1,0)
81                        ),
82           '1' => array(
83                    'A' => array(0,0,1,1,0,0,1),
84                    'C' => array(1,1,0,0,1,1,0)
85                        ),
86           '2' => array(
87                    'A' => array(0,0,1,0,0,1,1),
88                    'C' => array(1,1,0,1,1,0,0)
89                        ),
90           '3' => array(
91                    'A' => array(0,1,1,1,1,0,1),
92                    'C' => array(1,0,0,0,0,1,0)
93                        ),
94           '4' => array(
95                    'A' => array(0,1,0,0,0,1,1),
96                    'C' => array(1,0,1,1,1,0,0)
97                        ),
98           '5' => array(
99                    'A' => array(0,1,1,0,0,0,1),
100                    'C' => array(1,0,0,1,1,1,0)
101                        ),
102           '6' => array(
103                    'A' => array(0,1,0,1,1,1,1),
104                    'C' => array(1,0,1,0,0,0,0)
105                        ),
106           '7' => array(
107                    'A' => array(0,1,1,1,0,1,1),
108                    'C' => array(1,0,0,0,1,0,0)
109                        ),
110           '8' => array(
111                    'A' => array(0,1,1,0,1,1,1),
112                    'C' => array(1,0,0,1,0,0,0)
113                        ),
114           '9' => array(
115                    'A' => array(0,0,0,1,0,1,1),
116                    'C' => array(1,1,1,0,1,0,0)
117                        )
118        );
119
120    /**
121     * Draws a EAN 8 image barcode
122     *
123     * @param  string $text     A text that should be in the image barcode
124     * @param  string $imgtype  The image type that will be generated
125     *
126     * @return image            The corresponding EAN8 image barcode
127     *
128     * @access public
129     *
130     * @author     Tobias Frost tobi@coldtobi.de
131     * 			   based on the EAN13 code by Didier Fournout <didier.fournout@nyc.fr>
132     * @todo       Check if $text is number and len=8
133     *
134     */
135    function &draw($text, $imgtype = 'png')
136    {
137        // Calculate the barcode width
138        $barcodewidth = (strlen($text)) * (7 * $this->_barwidth)
139            + 3 * $this->_barwidth // left
140            + 5 * $this->_barwidth // center
141            + 3 * $this->_barwidth // right
142            ;
143
144        $barcodelongheight = (int) (imagefontheight($this->_font)/2) + $this->_barcodeheight;
145
146        // Create the image
147        $img = ImageCreate(
148                    $barcodewidth,
149                    $barcodelongheight + imagefontheight($this->_font) + 1
150                );
151
152        // Alocate the black and white colors
153        $black = ImageColorAllocate($img, 0, 0, 0);
154        $white = ImageColorAllocate($img, 255, 255, 255);
155
156        // Fill image with white color
157        imagefill($img, 0, 0, $white);
158
159        // Initiate x position
160        $xpos = 0;
161
162        // Draws the left guard pattern (bar-space-bar)
163        // bar
164        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
165        $xpos += $this->_barwidth;
166        // space
167        $xpos += $this->_barwidth;
168        // bar
169        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
170        $xpos += $this->_barwidth;
171
172        for ($idx = 0; $idx < 4; $idx ++) {
173            $value=substr($text,$idx,1);
174            imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black);
175            foreach ($this->_number_set[$value]['A'] as $bar) {
176                if ($bar) {
177                    imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black);
178                }
179                $xpos += $this->_barwidth;
180            }
181        }
182
183        // Draws the center pattern (space-bar-space-bar-space)
184        // space
185        $xpos += $this->_barwidth;
186        // bar
187        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
188        $xpos += $this->_barwidth;
189        // space
190        $xpos += $this->_barwidth;
191        // bar
192        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
193        $xpos += $this->_barwidth;
194        // space
195        $xpos += $this->_barwidth;
196
197
198        // Draw right $text contents
199        for ($idx = 4; $idx < 8; $idx ++) {
200            $value=substr($text,$idx,1);
201            imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black);
202            foreach ($this->_number_set[$value]['C'] as $bar) {
203                if ($bar) {
204                    imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black);
205                }
206                $xpos += $this->_barwidth;
207            }
208        }
209
210        // Draws the right guard pattern (bar-space-bar)
211        // bar
212        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
213        $xpos += $this->_barwidth;
214        // space
215        $xpos += $this->_barwidth;
216        // bar
217        imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
218        $xpos += $this->_barwidth;
219
220        return $img;
221    } // function create
222
223} // class
224?>