|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 //============================================================+ 00003 // File name : 2dbarcodes.php 00004 // Version : 1.0.012 00005 // Begin : 2009-04-07 00006 // Last Update : 2011-09-15 00007 // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com 00008 // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html) 00009 // ------------------------------------------------------------------- 00010 // Copyright (C) 2009-2011 Nicola Asuni - Tecnick.com S.r.l. 00011 // 00012 // This file is part of TCPDF software library. 00013 // 00014 // TCPDF is free software: you can redistribute it and/or modify it 00015 // under the terms of the GNU Lesser General Public License as 00016 // published by the Free Software Foundation, either version 3 of the 00017 // License, or (at your option) any later version. 00018 // 00019 // TCPDF is distributed in the hope that it will be useful, but 00020 // WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 // See the GNU Lesser General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU Lesser General Public License 00025 // along with TCPDF. If not, see <http://www.gnu.org/licenses/>. 00026 // 00027 // See LICENSE.TXT file for more information. 00028 // ------------------------------------------------------------------- 00029 // 00030 // Description : PHP class to creates array representations for 00031 // 2D barcodes to be used with TCPDF. 00032 // 00033 //============================================================+ 00034 00050 class TCPDF2DBarcode { 00051 00056 protected $barcode_array = false; 00057 00068 public function __construct($code, $type) { 00069 $this->setBarcode($code, $type); 00070 } 00071 00076 public function getBarcodeArray() { 00077 return $this->barcode_array; 00078 } 00079 00087 public function getBarcodeSVG($w=3, $h=3, $color='black') { 00088 // send headers 00089 $code = $this->getBarcodeSVGcode($w, $h, $color); 00090 header('Content-Type: application/svg+xml'); 00091 header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 00092 header('Pragma: public'); 00093 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past 00094 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 00095 header('Content-Disposition: inline; filename="'.md5($code).'.svg";'); 00096 //header('Content-Length: '.strlen($code)); 00097 echo $code; 00098 } 00099 00108 public function getBarcodeSVGcode($w=3, $h=3, $color='black') { 00109 // replace table for special characters 00110 $repstr = array("\0" => '', '&' => '&', '<' => '<', '>' => '>'); 00111 $svg = '<'.'?'.'xml version="1.0" standalone="no"'.'?'.'>'."\n"; 00112 $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'."\n"; 00113 $svg .= '<svg width="'.round(($this->barcode_array['num_cols'] * $w), 3).'" height="'.round(($this->barcode_array['num_rows'] * $h), 3).'" version="1.1" xmlns="http://www.w3.org/2000/svg">'."\n"; 00114 $svg .= "\t".'<desc>'.strtr($this->barcode_array['code'], $repstr).'</desc>'."\n"; 00115 $svg .= "\t".'<g id="elements" fill="'.$color.'" stroke="none">'."\n"; 00116 // print barcode elements 00117 $y = 0; 00118 // for each row 00119 for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { 00120 $x = 0; 00121 // for each column 00122 for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { 00123 if ($this->barcode_array['bcode'][$r][$c] == 1) { 00124 // draw a single barcode cell 00125 $svg .= "\t\t".'<rect x="'.$x.'" y="'.$y.'" width="'.$w.'" height="'.$h.'" />'."\n"; 00126 } 00127 $x += $w; 00128 } 00129 $y += $h; 00130 } 00131 $svg .= "\t".'</g>'."\n"; 00132 $svg .= '</svg>'."\n"; 00133 return $svg; 00134 } 00135 00144 public function getBarcodeHTML($w=10, $h=10, $color='black') { 00145 // replace table for special characters 00146 $html = '<div style="font-size:0;position:relative;">'."\n"; 00147 // print barcode elements 00148 $y = 0; 00149 // for each row 00150 for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { 00151 $x = 0; 00152 // for each column 00153 for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { 00154 if ($this->barcode_array['bcode'][$r][$c] == 1) { 00155 // draw a single barcode cell 00156 $html .= '<div style="background-color:'.$color.';width:'.$w.'px;height:'.$h.'px;position:absolute;left:'.$x.'px;top:'.$y.'px;"> </div>'."\n"; 00157 } 00158 $x += $w; 00159 } 00160 $y += $h; 00161 } 00162 $html .= '</div>'."\n"; 00163 return $html; 00164 } 00165 00174 public function getBarcodePNG($w=3, $h=3, $color=array(0,0,0)) { 00175 // calculate image size 00176 $width = ($this->barcode_array['num_cols'] * $w); 00177 $height = ($this->barcode_array['num_rows'] * $h); 00178 if (function_exists('imagecreate')) { 00179 // GD library 00180 $imagick = false; 00181 $png = imagecreate($width, $height); 00182 $bgcol = imagecolorallocate($png, 255, 255, 255); 00183 imagecolortransparent($png, $bgcol); 00184 $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]); 00185 } elseif (extension_loaded('imagick')) { 00186 $imagick = true; 00187 $bgcol = new imagickpixel('rgb(255,255,255'); 00188 $fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')'); 00189 $png = new Imagick(); 00190 $png->newImage($width, $height, 'none', 'png'); 00191 $bar = new imagickdraw(); 00192 $bar->setfillcolor($fgcol); 00193 } else { 00194 return false; 00195 } 00196 // print barcode elements 00197 $y = 0; 00198 // for each row 00199 for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { 00200 $x = 0; 00201 // for each column 00202 for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { 00203 if ($this->barcode_array['bcode'][$r][$c] == 1) { 00204 // draw a single barcode cell 00205 if ($imagick) { 00206 $bar->rectangle($x, $y, ($x + $w), ($y + $h)); 00207 } else { 00208 imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol); 00209 } 00210 } 00211 $x += $w; 00212 } 00213 $y += $h; 00214 } 00215 // send headers 00216 header('Content-Type: image/png'); 00217 header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 00218 header('Pragma: public'); 00219 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past 00220 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 00221 if ($imagick) { 00222 $png->drawimage($bar); 00223 echo $png; 00224 } else { 00225 imagepng($png); 00226 imagedestroy($png); 00227 } 00228 } 00229 00236 public function setBarcode($code, $type) { 00237 $mode = explode(',', $type); 00238 $qrtype = strtoupper($mode[0]); 00239 switch ($qrtype) { 00240 case 'DATAMATRIX': { // DATAMATRIX (ISO/IEC 16022) 00241 require_once(dirname(__FILE__).'/datamatrix.php'); 00242 $qrcode = new Datamatrix($code); 00243 $this->barcode_array = $qrcode->getBarcodeArray(); 00244 $this->barcode_array['code'] = $code; 00245 break; 00246 } 00247 case 'PDF417': { // PDF417 (ISO/IEC 15438:2006) 00248 require_once(dirname(__FILE__).'/pdf417.php'); 00249 if (!isset($mode[1]) OR ($mode[1] === '')) { 00250 $aspectratio = 2; // default aspect ratio (width / height) 00251 } else { 00252 $aspectratio = floatval($mode[1]); 00253 } 00254 if (!isset($mode[2]) OR ($mode[2] === '')) { 00255 $ecl = -1; // default error correction level (auto) 00256 } else { 00257 $ecl = intval($mode[2]); 00258 } 00259 // set macro block 00260 $macro = array(); 00261 if (isset($mode[3]) AND ($mode[3] !== '') AND isset($mode[4]) AND ($mode[4] !== '') AND isset($mode[5]) AND ($mode[5] !== '')) { 00262 $macro['segment_total'] = intval($mode[3]); 00263 $macro['segment_index'] = intval($mode[4]); 00264 $macro['file_id'] = strtr($mode[5], "\xff", ','); 00265 for ($i = 0; $i < 7; ++$i) { 00266 $o = $i + 6; 00267 if (isset($mode[$o]) AND ($mode[$o] !== '')) { 00268 // add option 00269 $macro['option_'.$i] = strtr($mode[$o], "\xff", ','); 00270 } 00271 } 00272 } 00273 $qrcode = new PDF417($code, $ecl, $aspectratio, $macro); 00274 $this->barcode_array = $qrcode->getBarcodeArray(); 00275 $this->barcode_array['code'] = $code; 00276 break; 00277 } 00278 case 'QRCODE': { // QR-CODE 00279 require_once(dirname(__FILE__).'/qrcode.php'); 00280 if (!isset($mode[1]) OR (!in_array($mode[1],array('L','M','Q','H')))) { 00281 $mode[1] = 'L'; // Ddefault: Low error correction 00282 } 00283 $qrcode = new QRcode($code, strtoupper($mode[1])); 00284 $this->barcode_array = $qrcode->getBarcodeArray(); 00285 $this->barcode_array['code'] = $code; 00286 break; 00287 } 00288 case 'RAW': 00289 case 'RAW2': { // RAW MODE 00290 // remove spaces 00291 $code = preg_replace('/[\s]*/si', '', $code); 00292 if (strlen($code) < 3) { 00293 break; 00294 } 00295 if ($qrtype == 'RAW') { 00296 // comma-separated rows 00297 $rows = explode(',', $code); 00298 } else { // RAW2 00299 // rows enclosed in square parentheses 00300 $code = substr($code, 1, -1); 00301 $rows = explode('][', $code); 00302 } 00303 $this->barcode_array['num_rows'] = count($rows); 00304 $this->barcode_array['num_cols'] = strlen($rows[0]); 00305 $this->barcode_array['bcode'] = array(); 00306 foreach ($rows as $r) { 00307 $this->barcode_array['bcode'][] = str_split($r, 1); 00308 } 00309 $this->barcode_array['code'] = $code; 00310 break; 00311 } 00312 case 'TEST': { // TEST MODE 00313 $this->barcode_array['num_rows'] = 5; 00314 $this->barcode_array['num_cols'] = 15; 00315 $this->barcode_array['bcode'] = array( 00316 array(1,1,1,0,1,1,1,0,1,1,1,0,1,1,1), 00317 array(0,1,0,0,1,0,0,0,1,0,0,0,0,1,0), 00318 array(0,1,0,0,1,1,0,0,1,1,1,0,0,1,0), 00319 array(0,1,0,0,1,0,0,0,0,0,1,0,0,1,0), 00320 array(0,1,0,0,1,1,1,0,1,1,1,0,0,1,0)); 00321 $this->barcode_array['code'] = $code; 00322 break; 00323 } 00324 default: { 00325 $this->barcode_array = false; 00326 } 00327 } 00328 } 00329 } // end of class 00330 00331 //============================================================+ 00332 // END OF FILE 00333 //============================================================+