imageWithLogo.php 2.9 KB

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