imagickWithLogo.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * ImageMagick with logo output example
  4. *
  5. * @created 28.02.2023
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2023 Smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpComposerExtensionStubsInspection
  11. */
  12. use chillerlan\QRCode\{QRCode, QRCodeException, QROptions};
  13. use chillerlan\QRCode\Common\EccLevel;
  14. use chillerlan\QRCode\Data\QRMatrix;
  15. use chillerlan\QRCode\Output\QRImagick;
  16. require_once __DIR__.'/../vendor/autoload.php';
  17. /*
  18. * Class definition
  19. */
  20. class QRImagickWithLogo extends QRImagick{
  21. /**
  22. * @inheritDoc
  23. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  24. */
  25. public function dump(string|null $file = null):string{
  26. // set returnResource to true to skip further processing for now
  27. $this->options->returnResource = true;
  28. // there's no need to save the result of dump() into $this->imagick here
  29. parent::dump($file);
  30. // set new logo size, leave a border of 1 module (no proportional resize/centering)
  31. $size = (($this->options->logoSpaceWidth - 2) * $this->options->scale);
  32. // logo position: the top left corner of the logo space
  33. $pos = (($this->moduleCount * $this->options->scale - $size) / 2);
  34. // invoke logo instance
  35. $imLogo = new Imagick($this->options->pngLogo);
  36. $imLogo->resizeImage($size, $size, Imagick::FILTER_LANCZOS, 0.85, true);
  37. // add the logo to the qrcode
  38. $this->imagick->compositeImage($imLogo, Imagick::COMPOSITE_BLEND, $pos, $pos);
  39. // output (retain functionality of the parent class)
  40. $imageData = $this->imagick->getImageBlob();
  41. $this->imagick->destroy();
  42. $this->saveToFile($imageData, $file);
  43. if($this->options->outputBase64){
  44. $imageData = $this->toBase64DataURI($imageData, $this->guessMimeType($imageData));
  45. }
  46. return $imageData;
  47. }
  48. }
  49. /**
  50. * augment the QROptions class
  51. *
  52. * @property string $pngLogo
  53. */
  54. class ImagickWithLogoOptions extends QROptions{
  55. protected string $pngLogo;
  56. /**
  57. * check logo
  58. *
  59. * of course, we could accept other formats too.
  60. * we're not checking for the file type either for simplicity reasons (assuming PNG)
  61. *
  62. * @throws \chillerlan\QRCode\QRCodeException
  63. */
  64. protected function set_pngLogo(string $pngLogo):void{
  65. if(!file_exists($pngLogo) || !is_file($pngLogo) || !is_readable($pngLogo)){
  66. throw new QRCodeException('invalid png logo');
  67. }
  68. // @todo: validate png
  69. $this->pngLogo = $pngLogo;
  70. }
  71. }
  72. /*
  73. * Runtime
  74. */
  75. $options = new ImagickWithLogoOptions;
  76. $options->pngLogo = __DIR__.'/octocat.png'; // setting from the augmented options
  77. $options->version = 5;
  78. $options->outputInterface = QRImagickWithLogo::class; // use the custom output class
  79. $options->eccLevel = EccLevel::H;
  80. $options->outputBase64 = false;
  81. $options->addLogoSpace = true;
  82. $options->logoSpaceWidth = 15;
  83. $options->logoSpaceHeight = 15;
  84. $options->bgColor = '#eee';
  85. $options->imageTransparent = true;
  86. $options->scale = 20;
  87. $options->drawLightModules = false;
  88. $options->drawCircularModules = true;
  89. $options->circleRadius = 0.4;
  90. $options->keepAsSquare = [
  91. QRMatrix::M_FINDER_DARK,
  92. QRMatrix::M_FINDER_DOT,
  93. QRMatrix::M_ALIGNMENT_DARK,
  94. ];
  95. // dump the output, with an additional logo
  96. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  97. header('Content-type: image/png');
  98. echo $out;
  99. exit;