imagickWithLogo.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 $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_ATOP, $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, (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData));
  45. }
  46. return $imageData;
  47. }
  48. }
  49. /**
  50. * augment the QROptions class
  51. */
  52. class ImagickWithLogoOptions extends QROptions{
  53. protected string $pngLogo;
  54. /**
  55. * check logo
  56. *
  57. * of course, we could accept other formats too.
  58. * we're not checking for the file type either for simplicity reasons (assuming PNG)
  59. */
  60. protected function set_pngLogo(string $pngLogo):void{
  61. if(!file_exists($pngLogo) || !is_file($pngLogo) || !is_readable($pngLogo)){
  62. throw new QRCodeException('invalid png logo');
  63. }
  64. // @todo: validate png
  65. $this->pngLogo = $pngLogo;
  66. }
  67. }
  68. /*
  69. * Runtime
  70. */
  71. $options = new ImagickWithLogoOptions;
  72. $options->pngLogo = __DIR__.'/octocat.png'; // setting from the augmented options
  73. $options->version = 5;
  74. $options->outputInterface = QRImagickWithLogo::class; // use the custom output class
  75. $options->eccLevel = EccLevel::H;
  76. $options->outputBase64 = false;
  77. $options->addLogoSpace = true;
  78. $options->logoSpaceWidth = 15;
  79. $options->logoSpaceHeight = 15;
  80. $options->bgColor = '#eee';
  81. $options->imageTransparent = true;
  82. $options->scale = 20;
  83. $options->drawLightModules = false;
  84. $options->drawCircularModules = true;
  85. $options->circleRadius = 0.4;
  86. $options->keepAsSquare = [
  87. QRMatrix::M_FINDER_DARK,
  88. QRMatrix::M_FINDER_DOT,
  89. QRMatrix::M_ALIGNMENT_DARK,
  90. ];
  91. // dump the output, with an additional logo
  92. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  93. header('Content-type: image/png');
  94. echo $out;
  95. exit;