1<?php
2/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
3
4/**
5 * Image_Barcode2_Driver_Int25 class
6 *
7 * Renders Interleaved 2 of 5 barcodes
8 *
9 * PHP versions 5
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_Barcode2
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 * @link      http://pear.php.net/package/Image_Barcode2
23 */
24
25require_once 'Image/Barcode2/Driver.php';
26require_once 'Image/Barcode2/Common.php';
27require_once 'Image/Barcode2/DualWidth.php';
28require_once 'Image/Barcode2/Exception.php';
29
30/**
31 * Interleaved 2 of 5
32 *
33 * Package which provides a method to create Interleaved 2 of 5
34 * barcode using GD library.
35 *
36 * @category  Image
37 * @package   Image_Barcode2
38 * @author    Marcelo Subtil Marcal <msmarcal@php.net>
39 * @copyright 2005 The PHP Group
40 * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
41 * @version   Release: @package_version@
42 * @link      http://pear.php.net/package/Image_Barcode2
43 */
44class Image_Barcode2_Driver_Int25 extends Image_Barcode2_Common implements Image_Barcode2_Driver, Image_Barcode2_DualWidth
45{
46    /**
47     * Coding map
48     * @var array
49     */
50    private $_codingmap = array(
51        '0' => '00110',
52        '1' => '10001',
53        '2' => '01001',
54        '3' => '11000',
55        '4' => '00101',
56        '5' => '10100',
57        '6' => '01100',
58        '7' => '00011',
59        '8' => '10010',
60        '9' => '01010'
61    );
62
63    /**
64     * Class constructor
65     *
66     * @param Image_Barcode2_Writer $writer Library to use.
67     */
68    public function __construct(Image_Barcode2_Writer $writer)
69    {
70        parent::__construct($writer);
71        $this->setBarcodeHeight(50);
72        $this->setBarcodeWidthThin(1);
73        $this->setBarcodeWidthThick(3);
74    }
75
76
77    /**
78     * Validate barcode
79     *
80     * @return void
81     * @throws Image_Barcode2_Exception
82     */
83    public function validate()
84    {
85        // Check barcode for invalid characters
86        if (!preg_match('/[0-9]/', $this->getBarcode())) {
87            throw new Image_Barcode2_Exception('Invalid barcode');
88        }
89    }
90
91
92    /**
93     * Draws a Interleaved 2 of 5 image barcode
94     *
95     * @return resource            The corresponding Interleaved 2 of 5 image barcode
96     *
97     * @access public
98     *
99     * @author Marcelo Subtil Marcal <msmarcal@php.net>
100     * @since  Image_Barcode2 0.3
101     */
102    public function draw()
103    {
104        $text   = $this->getBarcode();
105        $writer = $this->getWriter();
106
107        // if odd $text lenght adds a '0' at string beginning
108        $text = strlen($text) % 2 ? '0' . $text : $text;
109
110        // Calculate the barcode width
111        $barcodewidth = (strlen($text))
112            * (3 * $this->getBarcodeWidthThin() + 2 * $this->getBarcodeWidthThick())
113            + (strlen($text))
114            * 2.5
115            + (7 * $this->getBarcodeWidthThin() + $this->getBarcodeWidthThick()) + 3;
116
117        // Create the image
118        $img = $writer->imagecreate($barcodewidth, $this->getBarcodeHeight());
119
120        // Alocate the black and white colors
121        $black = $writer->imagecolorallocate($img, 0, 0, 0);
122        $white = $writer->imagecolorallocate($img, 255, 255, 255);
123
124        // Fill image with white color
125        $writer->imagefill($img, 0, 0, $white);
126
127        // Initiate x position
128        $xpos = 0;
129
130        // Draws the leader
131        for ($i = 0; $i < 2; $i++) {
132            $elementwidth = $this->getBarcodeWidthThin();
133            $writer->imagefilledrectangle(
134                $img,
135                $xpos,
136                0,
137                $xpos + $elementwidth - 1,
138                $this->getBarcodeHeight(),
139                $black
140            );
141            $xpos += $elementwidth;
142            $xpos += $this->getBarcodeWidthThin();
143            $xpos ++;
144        }
145
146        // Draw $text contents
147        $all = strlen($text);
148
149        // Draw 2 chars at a time
150        for ($idx = 0; $idx < $all; $idx += 2) {
151            $oddchar  = substr($text, $idx, 1);
152            $evenchar = substr($text, $idx + 1, 1);
153
154            // interleave
155            for ($baridx = 0; $baridx < 5; $baridx++) {
156
157                // Draws odd char corresponding bar (black)
158                $elementwidth = $this->getBarcodeWidthThin();
159                if (substr($this->_codingmap[$oddchar], $baridx, 1)) {
160                    $elementwidth = $this->getBarcodeWidthThick();
161                }
162
163                $writer->imagefilledrectangle(
164                    $img,
165                    $xpos,
166                    0,
167                    $xpos + $elementwidth - 1,
168                    $this->getBarcodeHeight(),
169                    $black
170                );
171
172                $xpos += $elementwidth;
173
174                // Left enought space to draw even char (white)
175                $elementwidth = $this->getBarcodeWidthThin();
176                if (substr($this->_codingmap[$evenchar], $baridx, 1)) {
177                    $elementwidth = $this->getBarcodeWidthThick();
178                }
179
180                $xpos += $elementwidth;
181                $xpos ++;
182            }
183        }
184
185
186        // Draws the trailer
187        $elementwidth = $this->getBarcodeWidthThick();
188        $writer->imagefilledrectangle(
189            $img,
190            $xpos,
191            0,
192            $xpos + $elementwidth - 1,
193            $this->getBarcodeHeight(),
194            $black
195        );
196        $xpos += $elementwidth;
197        $xpos += $this->getBarcodeWidthThin();
198        $xpos ++;
199        $elementwidth = $this->getBarcodeWidthThin();
200        $writer->imagefilledrectangle(
201            $img,
202            $xpos,
203            0,
204            $xpos + $elementwidth - 1,
205            $this->getBarcodeHeight(),
206            $black
207        );
208
209        return $img;
210    }
211
212} // class
213