1<?php 2/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ 3 4/** 5 * Image_Barcode2_Driver_Ean8 class 6 * 7 * Renders EAN 8 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 Tobias Frost <tobi@coldtobi.de> 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 * @link http://pear.php.net/package/Image_Barcode2 24 */ 25 26require_once 'Image/Barcode2/Driver.php'; 27require_once 'Image/Barcode2/Common.php'; 28require_once 'Image/Barcode2/Exception.php'; 29 30/** 31 * EAN 8 32 * 33 * Package which provides a method to create EAN 8 barcode using GD library. 34 * 35 * @category Image 36 * @package Image_Barcode2 37 * @author Tobias Frost <tobi@coldtobi.de> 38 * @author Didier Fournout <didier.fournout@nyc.fr> 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_Ean8 extends Image_Barcode2_Common implements Image_Barcode2_Driver 45{ 46 /** 47 * Coding map 48 * @var array 49 */ 50 private $_codingmap = array( 51 '0' => array( 52 'A' => array(0,0,0,1,1,0,1), 53 'C' => array(1,1,1,0,0,1,0) 54 ), 55 '1' => array( 56 'A' => array(0,0,1,1,0,0,1), 57 'C' => array(1,1,0,0,1,1,0) 58 ), 59 '2' => array( 60 'A' => array(0,0,1,0,0,1,1), 61 'C' => array(1,1,0,1,1,0,0) 62 ), 63 '3' => array( 64 'A' => array(0,1,1,1,1,0,1), 65 'C' => array(1,0,0,0,0,1,0) 66 ), 67 '4' => array( 68 'A' => array(0,1,0,0,0,1,1), 69 'C' => array(1,0,1,1,1,0,0) 70 ), 71 '5' => array( 72 'A' => array(0,1,1,0,0,0,1), 73 'C' => array(1,0,0,1,1,1,0) 74 ), 75 '6' => array( 76 'A' => array(0,1,0,1,1,1,1), 77 'C' => array(1,0,1,0,0,0,0) 78 ), 79 '7' => array( 80 'A' => array(0,1,1,1,0,1,1), 81 'C' => array(1,0,0,0,1,0,0) 82 ), 83 '8' => array( 84 'A' => array(0,1,1,0,1,1,1), 85 'C' => array(1,0,0,1,0,0,0) 86 ), 87 '9' => array( 88 'A' => array(0,0,0,1,0,1,1), 89 'C' => array(1,1,1,0,1,0,0) 90 ) 91 ); 92 93 /** 94 * Class constructor 95 * 96 * @param Image_Barcode2_Writer $writer Library to use. 97 */ 98 public function __construct(Image_Barcode2_Writer $writer) 99 { 100 parent::__construct($writer); 101 $this->setBarcodeHeight(50); 102 $this->setBarcodeWidth(1); 103 } 104 105 106 /** 107 * Validate barcode 108 * 109 * @return void 110 * @throws Image_Barcode2_Exception 111 */ 112 public function validate() 113 { 114 // Check barcode for invalid characters 115 if (!preg_match('/^[0-9]{8}$/', $this->getBarcode())) { 116 throw new Image_Barcode2_Exception('Invalid barcode'); 117 } 118 } 119 120 121 /** 122 * Draws a EAN 8 image barcode 123 * 124 * @return resource The corresponding EAN8 image barcode 125 * 126 * @access public 127 * 128 * @author Tobias Frost tobi@coldtobi.de 129 * based on the EAN13 code by Didier Fournout <didier.fournout@nyc.fr> 130 */ 131 public function draw() 132 { 133 $text = $this->getBarcode(); 134 $writer = $this->getWriter(); 135 $fontsize = $this->getFontSize(); 136 137 // Calculate the barcode width 138 $barcodewidth = (strlen($text)) * (7 * $this->getBarcodeWidth()) 139 + 3 * $this->getBarcodeWidth() // left 140 + 5 * $this->getBarcodeWidth() // center 141 + 3 * $this->getBarcodeWidth() // right 142 ; 143 144 $barcodelongheight = (int)($writer->imagefontheight($fontsize) / 2) 145 + $this->getBarcodeHeight(); 146 147 // Create the image 148 $img = $writer->imagecreate( 149 $barcodewidth, 150 $barcodelongheight + $writer->imagefontheight($fontsize) + 1 151 ); 152 153 // Alocate the black and white colors 154 $black = $writer->imagecolorallocate($img, 0, 0, 0); 155 $white = $writer->imagecolorallocate($img, 255, 255, 255); 156 157 // Fill image with white color 158 $writer->imagefill($img, 0, 0, $white); 159 160 // Initiate x position 161 $xpos = 0; 162 163 // Draws the left guard pattern (bar-space-bar) 164 // bar 165 $writer->imagefilledrectangle( 166 $img, 167 $xpos, 168 0, 169 $xpos + $this->getBarcodeWidth() - 1, 170 $barcodelongheight, 171 $black 172 ); 173 $xpos += $this->getBarcodeWidth(); 174 // space 175 $xpos += $this->getBarcodeWidth(); 176 // bar 177 $writer->imagefilledrectangle( 178 $img, 179 $xpos, 180 0, 181 $xpos + $this->getBarcodeWidth() - 1, 182 $barcodelongheight, 183 $black 184 ); 185 $xpos += $this->getBarcodeWidth(); 186 187 for ($idx = 0; $idx < 4; $idx ++) { 188 $value = substr($text, $idx, 1); 189 190 if ($this->showText) { 191 $writer->imagestring( 192 $img, 193 $fontsize, 194 $xpos + 1, 195 $this->getBarcodeHeight(), 196 $value, 197 $black 198 ); 199 } 200 201 foreach ($this->_codingmap[$value]['A'] as $bar) { 202 if ($bar) { 203 $writer->imagefilledrectangle( 204 $img, 205 $xpos, 206 0, 207 $xpos + $this->getBarcodeWidth() - 1, 208 $this->getBarcodeHeight(), 209 $black 210 ); 211 } 212 $xpos += $this->getBarcodeWidth(); 213 } 214 } 215 216 // Draws the center pattern (space-bar-space-bar-space) 217 // space 218 $xpos += $this->getBarcodeWidth(); 219 // bar 220 $writer->imagefilledrectangle( 221 $img, 222 $xpos, 223 0, 224 $xpos + $this->getBarcodeWidth() - 1, 225 $barcodelongheight, 226 $black 227 ); 228 229 $xpos += $this->getBarcodeWidth(); 230 // space 231 $xpos += $this->getBarcodeWidth(); 232 // bar 233 $writer->imagefilledrectangle( 234 $img, 235 $xpos, 236 0, 237 $xpos + $this->getBarcodeWidth() - 1, 238 $barcodelongheight, 239 $black 240 ); 241 242 $xpos += $this->getBarcodeWidth(); 243 // space 244 $xpos += $this->getBarcodeWidth(); 245 246 247 // Draw right $text contents 248 for ($idx = 4; $idx < 8; $idx ++) { 249 $value = substr($text, $idx, 1); 250 251 if ($this->showText) { 252 $writer->imagestring( 253 $img, 254 $fontsize, 255 $xpos + 1, 256 $this->getBarcodeHeight(), 257 $value, 258 $black 259 ); 260 } 261 262 foreach ($this->_codingmap[$value]['C'] as $bar) { 263 if ($bar) { 264 $writer->imagefilledrectangle( 265 $img, 266 $xpos, 267 0, 268 $xpos + $this->getBarcodeWidth() - 1, 269 $this->getBarcodeHeight(), 270 $black 271 ); 272 } 273 $xpos += $this->getBarcodeWidth(); 274 } 275 } 276 277 // Draws the right guard pattern (bar-space-bar) 278 // bar 279 $writer->imagefilledrectangle( 280 $img, 281 $xpos, 282 0, 283 $xpos + $this->getBarcodeWidth() - 1, 284 $barcodelongheight, 285 $black 286 ); 287 $xpos += $this->getBarcodeWidth(); 288 // space 289 $xpos += $this->getBarcodeWidth(); 290 // bar 291 $writer->imagefilledrectangle( 292 $img, 293 $xpos, 294 0, 295 $xpos + $this->getBarcodeWidth() - 1, 296 $barcodelongheight, 297 $black 298 ); 299 300 return $img; 301 } // function create 302 303} // class 304