imageWithLogo.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. $this->saveToFile($imageData, $file);
  49. if($this->options->imageBase64){
  50. $imageData = $this->toBase64DataURI($imageData, 'image/'.$this->options->outputType);
  51. }
  52. return $imageData;
  53. }
  54. }
  55. /*
  56. * Runtime
  57. */
  58. $options = new QROptions([
  59. 'version' => 5,
  60. 'eccLevel' => EccLevel::H,
  61. 'imageBase64' => false,
  62. 'addLogoSpace' => true,
  63. 'logoSpaceWidth' => 13,
  64. 'logoSpaceHeight' => 13,
  65. 'scale' => 6,
  66. 'imageTransparent' => false,
  67. 'drawCircularModules' => true,
  68. 'circleRadius' => 0.45,
  69. 'keepAsSquare' => [QRMatrix::M_FINDER, QRMatrix::M_FINDER_DOT],
  70. ]);
  71. $qrcode = new QRCode($options);
  72. $qrcode->addByteSegment('https://github.com');
  73. header('Content-type: image/png');
  74. $qrOutputInterface = new QRImageWithLogo($options, $qrcode->getMatrix());
  75. // dump the output, with an additional logo
  76. // the logo could also be supplied via the options, see the svgWithLogo example
  77. echo $qrOutputInterface->dump(null, __DIR__.'/octocat.png');
  78. exit;