Code25.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Barcode
  17. * @subpackage Object
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Barcode_Object_ObjectAbstract
  24. */
  25. require_once 'Zend/Barcode/Object/ObjectAbstract.php';
  26. /**
  27. * @see Zend_Validate_Barcode
  28. */
  29. require_once 'Zend/Validate/Barcode.php';
  30. /**
  31. * Class for generate Interleaved 2 of 5 barcode
  32. *
  33. * @category Zend
  34. * @package Zend_Barcode
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Barcode_Object_Code25 extends Zend_Barcode_Object_ObjectAbstract
  39. {
  40. /**
  41. * Coding map
  42. * - 0 = narrow bar
  43. * - 1 = wide bar
  44. * @var array
  45. */
  46. protected $_codingMap = array(
  47. '0' => '00110',
  48. '1' => '10001',
  49. '2' => '01001',
  50. '3' => '11000',
  51. '4' => '00101',
  52. '5' => '10100',
  53. '6' => '01100',
  54. '7' => '00011',
  55. '8' => '10010',
  56. '9' => '01010',
  57. );
  58. /**
  59. * Check allowed characters
  60. * @param string $value
  61. * @return string
  62. * @throw Zend_Barcode_Object_Exception
  63. */
  64. public function validateText($value)
  65. {
  66. $validator = new Zend_Validate_Barcode(array(
  67. 'adapter' => 'code25',
  68. 'checksum' => false,
  69. ));
  70. if (!$validator->isValid($value)) {
  71. $message = implode("\n", $validator->getMessages());
  72. /**
  73. * @see Zend_Barcode_Object_Exception
  74. */
  75. require_once 'Zend/Barcode/Object/Exception.php';
  76. throw new Zend_Barcode_Object_Exception($message);
  77. }
  78. }
  79. /**
  80. * Width of the barcode (in pixels)
  81. * @return integer
  82. */
  83. protected function _calculateBarcodeWidth()
  84. {
  85. $quietZone = $this->getQuietZone();
  86. $startCharacter = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor;
  87. $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth + 5 * $this->_barThinWidth)
  88. * $this->_factor;
  89. $encodedData = strlen($this->getText()) * $characterLength;
  90. $stopCharacter = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor;
  91. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  92. }
  93. /**
  94. * Partial check of interleaved 2 of 5 barcode
  95. * @return void
  96. */
  97. protected function _checkParams()
  98. {
  99. $this->_checkRatio();
  100. }
  101. /**
  102. * Prepare array to draw barcode
  103. * @return array
  104. */
  105. protected function _prepareBarcode()
  106. {
  107. $barcodeTable = array();
  108. // Start character (30301)
  109. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  110. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  111. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  112. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  113. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  114. $barcodeTable[] = array(0 , 1);
  115. $text = str_split($this->getText());
  116. foreach ($text as $char) {
  117. $barcodeChar = str_split($this->_codingMap[$char]);
  118. foreach ($barcodeChar as $c) {
  119. /* visible, width, top, length */
  120. $width = $c ? $this->_barThickWidth : $this->_barThinWidth;
  121. $barcodeTable[] = array(1 , $width , 0 , 1);
  122. $barcodeTable[] = array(0 , 1);
  123. }
  124. }
  125. // Stop character (30103)
  126. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  127. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  128. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  129. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  130. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  131. return $barcodeTable;
  132. }
  133. /**
  134. * Get barcode checksum
  135. *
  136. * @param string $text
  137. * @return int
  138. */
  139. public function getChecksum($text)
  140. {
  141. $this->_checkText($text);
  142. $factor = 3;
  143. $checksum = 0;
  144. for ($i = strlen($text); $i > 0; $i --) {
  145. $checksum += intval($text{$i - 1}) * $factor;
  146. $factor = 4 - $factor;
  147. }
  148. $checksum = 10 - ($checksum % 10);
  149. if ($checksum == 10) {
  150. $checksum = 0;
  151. }
  152. return $checksum;
  153. }
  154. /**
  155. * Retrieve text with checksum appended
  156. *
  157. * @return string
  158. */
  159. protected function _getTextWithChecksum()
  160. {
  161. return $this->_text . $this->getChecksum($this->_text);
  162. }
  163. }