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