Postnet.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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-2015 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 Postnet barcode
  32. *
  33. * @category Zend
  34. * @package Zend_Barcode
  35. * @copyright Copyright (c) 2005-2015 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_Postnet extends Zend_Barcode_Object_ObjectAbstract
  39. {
  40. /**
  41. * Coding map
  42. * - 0 = half bar
  43. * - 1 = complete bar
  44. * @var array
  45. */
  46. protected $_codingMap = array(
  47. 0 => "11000",
  48. 1 => "00011",
  49. 2 => "00101",
  50. 3 => "00110",
  51. 4 => "01001",
  52. 5 => "01010",
  53. 6 => "01100",
  54. 7 => "10001",
  55. 8 => "10010",
  56. 9 => "10100"
  57. );
  58. /**
  59. * Default options for Postnet barcode
  60. * @return void
  61. */
  62. protected function _getDefaultOptions()
  63. {
  64. $this->_barThinWidth = 2;
  65. $this->_barHeight = 20;
  66. $this->_drawText = false;
  67. $this->_stretchText = true;
  68. $this->_mandatoryChecksum = true;
  69. }
  70. /**
  71. * Width of the barcode (in pixels)
  72. * @return integer
  73. */
  74. protected function _calculateBarcodeWidth()
  75. {
  76. $quietZone = $this->getQuietZone();
  77. $startCharacter = (2 * $this->_barThinWidth) * $this->_factor;
  78. $stopCharacter = (1 * $this->_barThinWidth) * $this->_factor;
  79. $encodedData = (10 * $this->_barThinWidth) * $this->_factor * strlen($this->getText());
  80. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  81. }
  82. /**
  83. * Partial check of interleaved Postnet barcode
  84. * @return void
  85. */
  86. protected function _checkParams()
  87. {}
  88. /**
  89. * Prepare array to draw barcode
  90. * @return array
  91. */
  92. protected function _prepareBarcode()
  93. {
  94. $barcodeTable = array();
  95. // Start character (1)
  96. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  97. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  98. // Text to encode
  99. $textTable = str_split($this->getText());
  100. foreach ($textTable as $char) {
  101. $bars = str_split($this->_codingMap[$char]);
  102. foreach ($bars as $b) {
  103. $barcodeTable[] = array(1 , $this->_barThinWidth , 0.5 - $b * 0.5 , 1);
  104. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  105. }
  106. }
  107. // Stop character (1)
  108. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  109. return $barcodeTable;
  110. }
  111. /**
  112. * Get barcode checksum
  113. *
  114. * @param string $text
  115. * @return int
  116. */
  117. public function getChecksum($text)
  118. {
  119. $this->_checkText($text);
  120. $sum = array_sum(str_split($text));
  121. $checksum = (10 - ($sum % 10)) % 10;
  122. return $checksum;
  123. }
  124. }