imageWithLogo.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * GdImage with logo output example
  4. *
  5. * @created 18.11.2020
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2020 smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpComposerExtensionStubsInspection, PhpIllegalPsrClassPathInspection
  11. */
  12. declare(strict_types=1);
  13. use chillerlan\QRCode\{QRCode, QROptions};
  14. use chillerlan\QRCode\Common\EccLevel;
  15. use chillerlan\QRCode\Data\QRMatrix;
  16. use chillerlan\QRCode\Output\{QRGdImagePNG, QRCodeOutputException};
  17. require_once __DIR__.'/../vendor/autoload.php';
  18. /*
  19. * Class definition
  20. */
  21. class QRImageWithLogo extends QRGdImagePNG{
  22. /**
  23. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  24. */
  25. public function dump(string|null $file = null, string|null $logo = null):string{
  26. $logo ??= '';
  27. // set returnResource to true to skip further processing for now
  28. $this->options->returnResource = true;
  29. // of course, you could accept other formats too (such as resource or Imagick)
  30. // I'm not checking for the file type either for simplicity reasons (assuming PNG)
  31. if(!is_file($logo) || !is_readable($logo)){
  32. throw new QRCodeOutputException('invalid logo');
  33. }
  34. // there's no need to save the result of dump() into $this->image here
  35. parent::dump($file);
  36. $im = imagecreatefrompng($logo);
  37. if($im === false){
  38. throw new QRCodeOutputException('imagecreatefrompng() error');
  39. }
  40. // get logo image size
  41. $w = imagesx($im);
  42. $h = imagesy($im);
  43. // set new logo size, leave a border of 1 module (no proportional resize/centering)
  44. $lw = (($this->options->logoSpaceWidth - 2) * $this->options->scale);
  45. $lh = (($this->options->logoSpaceHeight - 2) * $this->options->scale);
  46. // get the qrcode size
  47. $ql = ($this->matrix->getSize() * $this->options->scale);
  48. // scale the logo and copy it over. done!
  49. imagecopyresampled($this->image, $im, (($ql - $lw) / 2), (($ql - $lh) / 2), 0, 0, $lw, $lh, $w, $h);
  50. $imageData = $this->dumpImage();
  51. $this->saveToFile($imageData, $file);
  52. if($this->options->outputBase64){
  53. $imageData = $this->toBase64DataURI($imageData);
  54. }
  55. return $imageData;
  56. }
  57. }
  58. /*
  59. * Runtime
  60. */
  61. $options = new QROptions;
  62. $options->version = 5;
  63. $options->outputBase64 = false;
  64. $options->scale = 6;
  65. $options->imageTransparent = false;
  66. $options->drawCircularModules = true;
  67. $options->circleRadius = 0.45;
  68. $options->keepAsSquare = [
  69. QRMatrix::M_FINDER,
  70. QRMatrix::M_FINDER_DOT,
  71. ];
  72. // ecc level H is required for logo space
  73. $options->eccLevel = EccLevel::H;
  74. $options->addLogoSpace = true;
  75. $options->logoSpaceWidth = 13;
  76. $options->logoSpaceHeight = 13;
  77. $qrcode = new QRCode($options);
  78. $qrcode->addByteSegment('https://github.com');
  79. $qrOutputInterface = new QRImageWithLogo($options, $qrcode->getQRMatrix());
  80. // dump the output, with an additional logo
  81. // the logo could also be supplied via the options, see the svgWithLogo example
  82. $out = $qrOutputInterface->dump(null, __DIR__.'/octocat.png');
  83. header('Content-type: image/png');
  84. echo $out;
  85. exit;