imagickWithLogo.php 3.2 KB

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