imagickWithLogo.php 3.2 KB

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