imagickWithLogo.php 3.3 KB

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