QRImagick.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Imagick, ImagickDraw, ImagickPixel;
  16. use function extension_loaded, is_string;
  17. /**
  18. * ImageMagick output module (requires ext-imagick)
  19. *
  20. * @see http://php.net/manual/book.imagick.php
  21. * @see http://phpimagick.com
  22. */
  23. class QRImagick extends QROutputAbstract{
  24. protected Imagick $imagick;
  25. protected ImagickDraw $imagickDraw;
  26. /**
  27. * @inheritDoc
  28. *
  29. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  30. */
  31. public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
  32. if(!extension_loaded('imagick')){
  33. throw new QRCodeOutputException('ext-imagick not loaded'); // @codeCoverageIgnore
  34. }
  35. parent::__construct($options, $matrix);
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. protected function setModuleValues():void{
  41. foreach($this::DEFAULT_MODULE_VALUES as $type => $defaultValue){
  42. $v = $this->options->moduleValues[$type] ?? null;
  43. if(!is_string($v)){
  44. $this->moduleValues[$type] = $defaultValue
  45. ? new ImagickPixel($this->options->markupDark)
  46. : new ImagickPixel($this->options->markupLight);
  47. }
  48. else{
  49. $this->moduleValues[$type] = new ImagickPixel($v);
  50. }
  51. }
  52. }
  53. /**
  54. * @inheritDoc
  55. *
  56. * @return string|\Imagick
  57. */
  58. public function dump(string $file = null){
  59. $file ??= $this->options->cachefile;
  60. $this->imagick = new Imagick;
  61. $this->imagick->newImage(
  62. $this->length,
  63. $this->length,
  64. new ImagickPixel($this->options->imagickBG ?? 'transparent'),
  65. $this->options->imagickFormat
  66. );
  67. $this->drawImage();
  68. if($this->options->returnResource){
  69. return $this->imagick;
  70. }
  71. $imageData = $this->imagick->getImageBlob();
  72. $this->imagick->destroy();
  73. if($file !== null){
  74. $this->saveToFile($imageData, $file);
  75. }
  76. return $imageData;
  77. }
  78. /**
  79. * Creates the QR image via ImagickDraw
  80. */
  81. protected function drawImage():void{
  82. $this->imagickDraw = new ImagickDraw;
  83. foreach($this->matrix->matrix() as $y => $row){
  84. foreach($row as $x => $M_TYPE){
  85. $this->setPixel($x, $y, $M_TYPE);
  86. }
  87. }
  88. $this->imagick->drawImage($this->imagickDraw);
  89. }
  90. /**
  91. * draws a single pixel at the given position
  92. */
  93. protected function setPixel(int $x, int $y, int $M_TYPE):void{
  94. $this->imagickDraw->setStrokeColor($this->moduleValues[$M_TYPE]);
  95. $this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);
  96. $this->options->drawCircularModules && !$this->matrix->checkTypes($x, $y, $this->options->keepAsSquare)
  97. ? $this->imagickDraw->circle(
  98. ($x + 0.5) * $this->scale,
  99. ($y + 0.5) * $this->scale,
  100. ($x + 0.5 + $this->options->circleRadius) * $this->scale,
  101. ($y + 0.5) * $this->scale
  102. )
  103. : $this->imagickDraw->rectangle(
  104. $x * $this->scale,
  105. $y * $this->scale,
  106. ($x + 1) * $this->scale,
  107. ($y + 1) * $this->scale
  108. );
  109. }
  110. }