/Zend/Barcode/Object/Code25.php

https://github.com/MontmereLimited/ZendFramework-v1 · PHP · 143 lines · 67 code · 13 blank · 63 comment · 1 complexity · 4b003adf213801ba96975a241e9eb430 MD5 · raw file

  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Code25.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * Width of the barcode (in pixels)
  60. * @return integer
  61. */
  62. protected function _calculateBarcodeWidth()
  63. {
  64. $quietZone = $this->getQuietZone();
  65. $startCharacter = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor;
  66. $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth + 5 * $this->_barThinWidth)
  67. * $this->_factor;
  68. $encodedData = strlen($this->getText()) * $characterLength;
  69. $stopCharacter = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor;
  70. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  71. }
  72. /**
  73. * Partial check of interleaved 2 of 5 barcode
  74. * @return void
  75. */
  76. protected function _checkParams()
  77. {
  78. $this->_checkRatio();
  79. }
  80. /**
  81. * Prepare array to draw barcode
  82. * @return array
  83. */
  84. protected function _prepareBarcode()
  85. {
  86. $barcodeTable = array();
  87. // Start character (30301)
  88. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  89. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  90. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  91. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  92. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  93. $barcodeTable[] = array(0 , $this->_barThinWidth);
  94. $text = str_split($this->getText());
  95. foreach ($text as $char) {
  96. $barcodeChar = str_split($this->_codingMap[$char]);
  97. foreach ($barcodeChar as $c) {
  98. /* visible, width, top, length */
  99. $width = $c ? $this->_barThickWidth : $this->_barThinWidth;
  100. $barcodeTable[] = array(1 , $width , 0 , 1);
  101. $barcodeTable[] = array(0 , $this->_barThinWidth);
  102. }
  103. }
  104. // Stop character (30103)
  105. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  106. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  107. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  108. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  109. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  110. return $barcodeTable;
  111. }
  112. /**
  113. * Get barcode checksum
  114. *
  115. * @param string $text
  116. * @return int
  117. */
  118. public function getChecksum($text)
  119. {
  120. $this->_checkText($text);
  121. $factor = 3;
  122. $checksum = 0;
  123. for ($i = strlen($text); $i > 0; $i --) {
  124. $checksum += intval($text{$i - 1}) * $factor;
  125. $factor = 4 - $factor;
  126. }
  127. $checksum = (10 - ($checksum % 10)) % 10;
  128. return $checksum;
  129. }
  130. }