Ean5.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_Ean13
  24. */
  25. require_once 'Zend/Barcode/Object/Ean13.php';
  26. /**
  27. * @see Zend_Validate_Barcode
  28. */
  29. require_once 'Zend/Validate/Barcode.php';
  30. /**
  31. * Class for generate Ean5 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_Ean5 extends Zend_Barcode_Object_Ean13
  39. {
  40. protected $_parities = array(
  41. 0 => array('B','B','A','A','A'),
  42. 1 => array('B','A','B','A','A'),
  43. 2 => array('B','A','A','B','A'),
  44. 3 => array('B','A','A','A','B'),
  45. 4 => array('A','B','B','A','A'),
  46. 5 => array('A','A','B','B','A'),
  47. 6 => array('A','A','A','B','B'),
  48. 7 => array('A','B','A','B','A'),
  49. 8 => array('A','B','A','A','B'),
  50. 9 => array('A','A','B','A','B')
  51. );
  52. /**
  53. * Default options for Ean5 barcode
  54. * @return void
  55. */
  56. protected function _getDefaultOptions()
  57. {
  58. $this->_barcodeLength = 5;
  59. }
  60. /**
  61. * Width of the barcode (in pixels)
  62. * @return integer
  63. */
  64. protected function _calculateBarcodeWidth()
  65. {
  66. $quietZone = $this->getQuietZone();
  67. $startCharacter = (5 * $this->_barThinWidth) * $this->_factor;
  68. $middleCharacter = (2 * $this->_barThinWidth) * $this->_factor;
  69. $encodedData = (7 * $this->_barThinWidth) * $this->_factor;
  70. return $quietZone + $startCharacter + ($this->_barcodeLength - 1) * $middleCharacter + $this->_barcodeLength * $encodedData + $quietZone;
  71. }
  72. /**
  73. * Prepare array to draw barcode
  74. * @return array
  75. */
  76. protected function _prepareBarcode()
  77. {
  78. $barcodeTable = array();
  79. // Start character (01011)
  80. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  81. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  82. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  83. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  84. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  85. $firstCharacter = true;
  86. $textTable = str_split($this->getText());
  87. // Characters
  88. for ($i = 0; $i < $this->_barcodeLength; $i++) {
  89. if ($firstCharacter) {
  90. $firstCharacter = false;
  91. } else {
  92. // Intermediate character (01)
  93. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  94. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  95. }
  96. $bars = str_split($this->_codingMap[$this->_getParity($i)][$textTable[$i]]);
  97. foreach ($bars as $b) {
  98. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  99. }
  100. }
  101. return $barcodeTable;
  102. }
  103. /**
  104. * Get barcode checksum
  105. *
  106. * @param string $text
  107. * @return int
  108. */
  109. public function getChecksum($text)
  110. {
  111. $this->_checkText($text);
  112. $checksum = 0;
  113. for ($i = 0 ; $i < $this->_barcodeLength; $i ++) {
  114. $checksum += intval($text{$i}) * ($i % 2 ? 9 : 3);
  115. }
  116. return ($checksum % 10);
  117. }
  118. protected function _getParity($i)
  119. {
  120. $checksum = $this->getChecksum($this->getText());
  121. return $this->_parities[$checksum][$i];
  122. }
  123. /**
  124. * Retrieve text to encode
  125. * @return string
  126. */
  127. public function getText()
  128. {
  129. return $this->_addLeadingZeros($this->_text);
  130. }
  131. }