人人商城

BCGupca.barcode.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - UPC-A
  6. *
  7. * UPC-A contains
  8. * - 2 system digits (1 not provided, a 0 is added automatically)
  9. * - 5 manufacturer code digits
  10. * - 5 product digits
  11. * - 1 checksum digit
  12. *
  13. * The checksum is always displayed.
  14. *
  15. *--------------------------------------------------------------------
  16. * Copyright (C) Jean-Sebastien Goupil
  17. * http://www.barcodephp.com
  18. */
  19. include_once('BCGParseException.php');
  20. include_once('BCGBarcode.php');
  21. include_once('BCGean13.barcode.php');
  22. include_once('BCGLabel.php');
  23. class BCGupca extends BCGean13 {
  24. protected $labelRight = null;
  25. /**
  26. * Constructor.
  27. */
  28. public function __construct() {
  29. parent::__construct();
  30. }
  31. /**
  32. * Draws the barcode.
  33. *
  34. * @param resource $im
  35. */
  36. public function draw($im) {
  37. // The following code is exactly the same as EAN13. We just add a 0 in front of the code !
  38. $this->text = '0' . $this->text; // We will remove it at the end... don't worry
  39. parent::draw($im);
  40. // We remove the 0 in front, as we said :)
  41. $this->text = substr($this->text, 1);
  42. }
  43. /**
  44. * Draws the extended bars on the image.
  45. *
  46. * @param resource $im
  47. * @param int $plus
  48. */
  49. protected function drawExtendedBars($im, $plus) {
  50. $temp_text = $this->text . $this->keys[$this->checksumValue];
  51. $rememberX = $this->positionX;
  52. $rememberH = $this->thickness;
  53. // We increase the bars
  54. // First 2 Bars
  55. $this->thickness = $this->thickness + intval($plus / $this->scale);
  56. $this->positionX = 0;
  57. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  58. $this->positionX += 2;
  59. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  60. // Attemping to increase the 2 following bars
  61. $this->positionX += 1;
  62. $temp_value = $this->findCode($temp_text[1]);
  63. $this->drawChar($im, $temp_value, false);
  64. // Center Guard Bar
  65. $this->positionX += 36;
  66. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  67. $this->positionX += 2;
  68. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  69. // Attemping to increase the 2 last bars
  70. $this->positionX += 37;
  71. $temp_value = $this->findCode($temp_text[12]);
  72. $this->drawChar($im, $temp_value, true);
  73. // Completly last bars
  74. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  75. $this->positionX += 2;
  76. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  77. $this->positionX = $rememberX;
  78. $this->thickness = $rememberH;
  79. }
  80. /**
  81. * Adds the default label.
  82. */
  83. protected function addDefaultLabel() {
  84. if ($this->isDefaultEanLabelEnabled()) {
  85. $this->processChecksum();
  86. $label = $this->getLabel();
  87. $font = $this->font;
  88. $this->labelLeft = new BCGLabel(substr($label, 0, 1), $font, BCGLabel::POSITION_LEFT, BCGLabel::ALIGN_BOTTOM);
  89. $this->labelLeft->setSpacing(4 * $this->scale);
  90. $this->labelCenter1 = new BCGLabel(substr($label, 1, 5), $font, BCGLabel::POSITION_BOTTOM, BCGLabel::ALIGN_LEFT);
  91. $labelCenter1Dimension = $this->labelCenter1->getDimension();
  92. $this->labelCenter1->setOffset(($this->scale * 44 - $labelCenter1Dimension[0]) / 2 + $this->scale * 6);
  93. $this->labelCenter2 = new BCGLabel(substr($label, 6, 5), $font, BCGLabel::POSITION_BOTTOM, BCGLabel::ALIGN_LEFT);
  94. $this->labelCenter2->setOffset(($this->scale * 44 - $labelCenter1Dimension[0]) / 2 + $this->scale * 45);
  95. $this->labelRight = new BCGLabel($this->keys[$this->checksumValue], $font, BCGLabel::POSITION_RIGHT, BCGLabel::ALIGN_BOTTOM);
  96. $this->labelRight->setSpacing(4 * $this->scale);
  97. if ($this->alignLabel) {
  98. $labelDimension = $this->labelCenter1->getDimension();
  99. $this->labelLeft->setOffset($labelDimension[1]);
  100. $this->labelRight->setOffset($labelDimension[1]);
  101. } else {
  102. $labelDimension = $this->labelLeft->getDimension();
  103. $this->labelLeft->setOffset($labelDimension[1] / 2);
  104. $labelDimension = $this->labelLeft->getDimension();
  105. $this->labelRight->setOffset($labelDimension[1] / 2);
  106. }
  107. $this->addLabel($this->labelLeft);
  108. $this->addLabel($this->labelCenter1);
  109. $this->addLabel($this->labelCenter2);
  110. $this->addLabel($this->labelRight);
  111. }
  112. }
  113. /**
  114. * Check correct length.
  115. */
  116. protected function checkCorrectLength() {
  117. // If we have 12 chars, just flush the last one without throwing anything
  118. $c = strlen($this->text);
  119. if ($c === 12) {
  120. $this->text = substr($this->text, 0, 11);
  121. } elseif ($c !== 11) {
  122. throw new BCGParseException('upca', 'Must contain 11 digits, the 12th digit is automatically added.');
  123. }
  124. }
  125. }
  126. ?>