QRImagick.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Class QRImagick
  4. *
  5. * @created 04.07.2018
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2018 smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpComposerExtensionStubsInspection
  11. */
  12. namespace chillerlan\QRCode\Output;
  13. use chillerlan\QRCode\Data\QRMatrix;
  14. use chillerlan\Settings\SettingsContainerInterface;
  15. use finfo, Imagick, ImagickDraw, ImagickPixel;
  16. use function extension_loaded, is_string;
  17. use const FILEINFO_MIME_TYPE;
  18. /**
  19. * ImageMagick output module (requires ext-imagick)
  20. *
  21. * @see http://php.net/manual/book.imagick.php
  22. * @see http://phpimagick.com
  23. */
  24. class QRImagick extends QROutputAbstract{
  25. protected Imagick $imagick;
  26. protected ImagickDraw $imagickDraw;
  27. /**
  28. * @inheritDoc
  29. *
  30. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  31. */
  32. public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
  33. if(!extension_loaded('imagick')){
  34. throw new QRCodeOutputException('ext-imagick not loaded'); // @codeCoverageIgnore
  35. }
  36. if(!extension_loaded('fileinfo')){
  37. throw new QRCodeOutputException('ext-fileinfo not loaded'); // @codeCoverageIgnore
  38. }
  39. parent::__construct($options, $matrix);
  40. }
  41. /**
  42. * @inheritDoc
  43. */
  44. protected function moduleValueIsValid($value):bool{
  45. return is_string($value);
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. protected function getModuleValue($value):ImagickPixel{
  51. return new ImagickPixel($value);
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
  57. return new ImagickPixel(($isDark) ? $this->options->markupDark : $this->options->markupLight);
  58. }
  59. /**
  60. * @inheritDoc
  61. *
  62. * @return string|\Imagick
  63. */
  64. public function dump(string $file = null){
  65. $this->imagick = new Imagick;
  66. $bgColor = ($this->options->imageTransparent) ? 'transparent' : 'white';
  67. // keep the imagickBG property for now (until v6)
  68. if($this->moduleValueIsValid($this->options->bgColor ?? $this->options->imagickBG)){
  69. $bgColor = ($this->options->bgColor ?? $this->options->imagickBG);
  70. }
  71. $this->imagick->newImage($this->length, $this->length, new ImagickPixel($bgColor), $this->options->imagickFormat);
  72. $this->drawImage();
  73. if($this->options->returnResource){
  74. return $this->imagick;
  75. }
  76. $imageData = $this->imagick->getImageBlob();
  77. $this->imagick->destroy();
  78. $this->saveToFile($imageData, $file);
  79. if($this->options->imageBase64){
  80. $imageData = $this->toBase64DataURI($imageData, (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData));
  81. }
  82. return $imageData;
  83. }
  84. /**
  85. * Creates the QR image via ImagickDraw
  86. */
  87. protected function drawImage():void{
  88. $this->imagickDraw = new ImagickDraw;
  89. $this->imagickDraw->setStrokeWidth(0);
  90. foreach($this->matrix->matrix() as $y => $row){
  91. foreach($row as $x => $M_TYPE){
  92. $this->setPixel($x, $y, $M_TYPE);
  93. }
  94. }
  95. $this->imagick->drawImage($this->imagickDraw);
  96. }
  97. /**
  98. * draws a single pixel at the given position
  99. */
  100. protected function setPixel(int $x, int $y, int $M_TYPE):void{
  101. if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
  102. return;
  103. }
  104. $this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);
  105. $this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
  106. ? $this->imagickDraw->circle(
  107. (($x + 0.5) * $this->scale),
  108. (($y + 0.5) * $this->scale),
  109. (($x + 0.5 + $this->options->circleRadius) * $this->scale),
  110. (($y + 0.5) * $this->scale)
  111. )
  112. : $this->imagickDraw->rectangle(
  113. ($x * $this->scale),
  114. ($y * $this->scale),
  115. (($x + 1) * $this->scale),
  116. (($y + 1) * $this->scale)
  117. );
  118. }
  119. }