imageWithLogo.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @created 18.11.2020
  4. * @author smiley <smiley@chillerlan.net>
  5. * @copyright 2020 smiley
  6. * @license MIT
  7. *
  8. * @noinspection PhpComposerExtensionStubsInspection, PhpIllegalPsrClassPathInspection
  9. */
  10. use chillerlan\QRCode\{QRCode, QROptions};
  11. use chillerlan\QRCode\Common\EccLevel;
  12. use chillerlan\QRCode\Data\QRMatrix;
  13. use chillerlan\QRCode\Output\{QRGdImage, QRCodeOutputException};
  14. require_once __DIR__.'/../vendor/autoload.php';
  15. /*
  16. * Class definition
  17. */
  18. class QRImageWithLogo extends QRGdImage{
  19. /**
  20. * @param string|null $file
  21. * @param string|null $logo
  22. *
  23. * @return string
  24. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  25. */
  26. public function dump(string $file = null, string $logo = null):string{
  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. // get logo image size
  38. $w = imagesx($im);
  39. $h = imagesy($im);
  40. // set new logo size, leave a border of 1 module (no proportional resize/centering)
  41. $lw = ($this->options->logoSpaceWidth - 2) * $this->options->scale;
  42. $lh = ($this->options->logoSpaceHeight - 2) * $this->options->scale;
  43. // get the qrcode size
  44. $ql = $this->matrix->size() * $this->options->scale;
  45. // scale the logo and copy it over. done!
  46. imagecopyresampled($this->image, $im, ($ql - $lw) / 2, ($ql - $lh) / 2, 0, 0, $lw, $lh, $w, $h);
  47. $imageData = $this->dumpImage();
  48. if($file !== null){
  49. $this->saveToFile($imageData, $file);
  50. }
  51. if($this->options->imageBase64){
  52. $imageData = $this->base64encode($imageData, 'image/'.$this->options->outputType);
  53. }
  54. return $imageData;
  55. }
  56. }
  57. /*
  58. * Runtime
  59. */
  60. $options = new QROptions([
  61. 'version' => 5,
  62. 'eccLevel' => EccLevel::H,
  63. 'imageBase64' => false,
  64. 'addLogoSpace' => true,
  65. 'logoSpaceWidth' => 13,
  66. 'logoSpaceHeight' => 13,
  67. 'scale' => 6,
  68. 'imageTransparent' => false,
  69. 'drawCircularModules' => true,
  70. 'circleRadius' => 0.45,
  71. 'keepAsSquare' => [QRMatrix::M_FINDER, QRMatrix::M_FINDER_DOT],
  72. ]);
  73. $qrcode = new QRCode($options);
  74. $qrcode->addByteSegment('https://github.com');
  75. header('Content-type: image/png');
  76. $qrOutputInterface = new QRImageWithLogo($options, $qrcode->getMatrix());
  77. // dump the output, with an additional logo
  78. // the logo could also be supplied via the options, see the svgWithLogo example
  79. echo $qrOutputInterface->dump(null, __DIR__.'/octocat.png');
  80. exit;