2dbarcodes.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. //============================================================+
  3. // File name : 2dbarcodes.php
  4. // Begin : 2009-04-07
  5. // Last Update : 2010-04-30
  6. // Version : 1.0.003
  7. // License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
  8. // ----------------------------------------------------------------------------
  9. // Copyright (C) 2008-2009 Nicola Asuni - Tecnick.com S.r.l.
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU Lesser General Public License as published by
  13. // the Free Software Foundation, either version 2.1 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU Lesser General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU Lesser General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. // See LICENSE.TXT file for more information.
  25. // ----------------------------------------------------------------------------
  26. //
  27. // Description : PHP class to creates array representations for
  28. // 2D barcodes to be used with TCPDF.
  29. //
  30. // Author: Nicola Asuni
  31. //
  32. // (c) Copyright:
  33. // Nicola Asuni
  34. // Tecnick.com S.r.l.
  35. // Via della Pace, 11
  36. // 09044 Quartucciu (CA)
  37. // ITALY
  38. // www.tecnick.com
  39. // info@tecnick.com
  40. //============================================================+
  41. /**
  42. * PHP class to creates array representations for 2D barcodes to be used with TCPDF.
  43. * @package com.tecnick.tcpdf
  44. * @abstract Functions for generating string representation of 2D barcodes.
  45. * @author Nicola Asuni
  46. * @copyright 2008-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  47. * @link http://www.tcpdf.org
  48. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  49. * @version 1.0.003
  50. */
  51. /**
  52. * PHP class to creates array representations for 2D barcodes to be used with TCPDF (http://www.tcpdf.org).<br>
  53. * @name TCPDFBarcode
  54. * @package com.tecnick.tcpdf
  55. * @version 1.0.003
  56. * @author Nicola Asuni
  57. * @link http://www.tcpdf.org
  58. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  59. */
  60. class TCPDF2DBarcode {
  61. /**
  62. * @var array representation of barcode.
  63. * @access protected
  64. */
  65. protected $barcode_array = false;
  66. /**
  67. * This is the class constructor.
  68. * Return an array representations for 2D barcodes:<ul>
  69. * <li>$arrcode['code'] code to be printed on text label</li>
  70. * <li>$arrcode['num_rows'] required number of rows</li>
  71. * <li>$arrcode['num_cols'] required number of columns</li>
  72. * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
  73. * @param string $code code to print
  74. * @param string $type type of barcode: <ul>li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>QRCODE : QR-CODE Low error correction</li><li>QRCODE,L : QR-CODE Low error correction</li><li>QRCODE,M : QR-CODE Medium error correction</li><li>QRCODE,Q : QR-CODE Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li></ul>
  75. */
  76. public function __construct($code, $type) {
  77. $this->setBarcode($code, $type);
  78. }
  79. /**
  80. * Return an array representations of barcode.
  81. * @return array
  82. */
  83. public function getBarcodeArray() {
  84. return $this->barcode_array;
  85. }
  86. /**
  87. * Set the barcode.
  88. * @param string $code code to print
  89. * @param string $type type of barcode: <ul><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>QRCODE : QR-CODE Low error correction</li><li>QRCODE,L : QR-CODE Low error correction</li><li>QRCODE,M : QR-CODE Medium error correction</li><li>QRCODE,Q : QR-CODE Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li></ul>
  90. * @return array
  91. */
  92. public function setBarcode($code, $type) {
  93. $mode = explode(',', $type);
  94. $qrtype = strtoupper($mode[0]);
  95. switch ($qrtype) {
  96. case 'QRCODE': { // QR-CODE
  97. require_once(dirname(__FILE__).'/qrcode.php');
  98. if (!isset($mode[1]) OR (!in_array($mode[1],array('L','M','Q','H')))) {
  99. $mode[1] = 'L'; // Ddefault: Low error correction
  100. }
  101. $qrcode = new QRcode($code, strtoupper($mode[1]));
  102. $this->barcode_array = $qrcode->getBarcodeArray();
  103. break;
  104. }
  105. case 'RAW':
  106. case 'RAW2': { // RAW MODE
  107. // remove spaces
  108. $code = preg_replace('/[\s]*/si', '', $code);
  109. if (strlen($code) < 3) {
  110. break;
  111. }
  112. if ($qrtype == 'RAW') {
  113. // comma-separated rows
  114. $rows = explode(',', $code);
  115. } else {
  116. // rows enclosed in square parethesis
  117. $code = substr($code, 1, -1);
  118. $rows = explode('][', $code);
  119. }
  120. $this->barcode_array['num_rows'] = count($rows);
  121. $this->barcode_array['num_cols'] = strlen($rows[0]);
  122. $this->barcode_array['bcode'] = array();
  123. foreach ($rows as $r) {
  124. $this->barcode_array['bcode'][] = str_split($r, 1);
  125. }
  126. break;
  127. }
  128. case 'TEST': { // TEST MODE
  129. $this->barcode_array['num_rows'] = 5;
  130. $this->barcode_array['num_cols'] = 15;
  131. $this->barcode_array['bcode'] = array(
  132. array(1,1,1,0,1,1,1,0,1,1,1,0,1,1,1),
  133. array(0,1,0,0,1,0,0,0,1,0,0,0,0,1,0),
  134. array(0,1,0,0,1,1,0,0,1,1,1,0,0,1,0),
  135. array(0,1,0,0,1,0,0,0,0,0,1,0,0,1,0),
  136. array(0,1,0,0,1,1,1,0,1,1,1,0,0,1,0));
  137. break;
  138. }
  139. default: {
  140. $this->barcode_array = false;
  141. }
  142. }
  143. }
  144. } // end of class
  145. //============================================================+
  146. // END OF FILE
  147. //============================================================+
  148. ?>