BarcodeGeneratorPNG.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\librarys\Picqer\Barcode;
  8. use Imagick;
  9. use imagickdraw;
  10. use imagickpixel;
  11. use app\librarys\Picqer\Barcode\Exceptions\BarcodeException;
  12. class BarcodeGeneratorPNG extends BarcodeGenerator
  13. {
  14. protected $useImagick = true;
  15. public function __construct()
  16. {
  17. // Auto switch between GD and Imagick based on what is installed
  18. if (extension_loaded('imagick')) {
  19. $this->useImagick = true;
  20. } elseif (function_exists('imagecreate')) {
  21. $this->useImagick = false;
  22. } else {
  23. throw new BarcodeException('Neither gd-lib or imagick are installed!');
  24. }
  25. }
  26. /**
  27. * Force the use of Imagick image extension
  28. */
  29. public function useImagick()
  30. {
  31. $this->useImagick = true;
  32. }
  33. /**
  34. * Force the use of the GD image library
  35. */
  36. public function useGd()
  37. {
  38. $this->useImagick = false;
  39. }
  40. /**
  41. * Return a PNG image representation of barcode (requires GD or Imagick library).
  42. *
  43. * @param string $barcode code to print
  44. * @param string $type type of barcode:
  45. * @param int $widthFactor Width of a single bar element in pixels.
  46. * @param int $height Height of a single bar element in pixels.
  47. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent).
  48. * @return string image data or false in case of error.
  49. */
  50. public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0])
  51. {
  52. $barcodeData = $this->getBarcodeData($barcode, $type);
  53. $width = round($barcodeData->getWidth() * $widthFactor);
  54. if ($this->useImagick) {
  55. $imagickBarsShape = new imagickdraw();
  56. $imagickBarsShape->setFillColor(new imagickpixel('rgb(' . implode(',', $foregroundColor) .')'));
  57. } else {
  58. $image = $this->createGdImageObject($width, $height);
  59. $gdForegroundColor = imagecolorallocate($image, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]);
  60. }
  61. // print bars
  62. $positionHorizontal = 0;
  63. /** @var BarcodeBar $bar */
  64. foreach ($barcodeData->getBars() as $bar) {
  65. $barWidth = round(($bar->getWidth() * $widthFactor), 3);
  66. if ($bar->isBar() && $barWidth > 0) {
  67. $y = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3);
  68. $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3);
  69. // draw a vertical bar
  70. if ($this->useImagick && isset($imagickBarsShape)) {
  71. $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight));
  72. } else {
  73. imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor);
  74. }
  75. }
  76. $positionHorizontal += $barWidth;
  77. }
  78. if ($this->useImagick && isset($imagickBarsShape)) {
  79. $image = $this->createImagickImageObject($width, $height);
  80. $image->drawImage($imagickBarsShape);
  81. return $image->getImageBlob();
  82. }
  83. ob_start();
  84. $this->generateGdImage($image);
  85. return ob_get_clean();
  86. }
  87. protected function createGdImageObject(int $width, int $height)
  88. {
  89. $image = imagecreate($width, $height);
  90. $colorBackground = imagecolorallocate($image, 255, 255, 255);
  91. imagecolortransparent($image, $colorBackground);
  92. return $image;
  93. }
  94. protected function createImagickImageObject(int $width, int $height): Imagick
  95. {
  96. $image = new Imagick();
  97. $image->newImage($width, $height, 'none', 'PNG');
  98. return $image;
  99. }
  100. protected function generateGdImage($image)
  101. {
  102. imagepng($image);
  103. imagedestroy($image);
  104. }
  105. }