imageWithLogo.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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
  9. */
  10. use chillerlan\QRCode\{QRCode, QROptions};
  11. use chillerlan\QRCode\Output\{QRCodeOutputException, QRImage};
  12. require_once __DIR__.'/../vendor/autoload.php';
  13. class QRImageWithLogo extends QRImage{
  14. /**
  15. * @param string|null $file
  16. * @param string|null $logo
  17. *
  18. * @return string
  19. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  20. */
  21. public function dump(?string $file = null, ?string $logo = null):string{
  22. // set returnResource to true to skip further processing for now
  23. $this->options->returnResource = true;
  24. // of course you could accept other formats too (such as resource or Imagick)
  25. // i'm not checking for the file type either for simplicity reasons (assuming PNG)
  26. if(!is_file($logo) || !is_readable($logo)){
  27. throw new QRCodeOutputException('invalid logo');
  28. }
  29. $this->matrix->setLogoSpace(
  30. $this->options->logoSpaceWidth,
  31. $this->options->logoSpaceHeight
  32. // not utilizing the position here
  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 = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
  53. }
  54. return $imageData;
  55. }
  56. }
  57. /**
  58. * @property int $logoSpaceWidth
  59. * @property int $logoSpaceHeight
  60. */
  61. class LogoOptions extends QROptions{
  62. // size in QR modules, multiply with QROptions::$scale for pixel size
  63. protected int $logoSpaceWidth;
  64. protected int $logoSpaceHeight;
  65. }
  66. $options = new LogoOptions;
  67. $options->version = 7;
  68. $options->eccLevel = QRCode::ECC_H;
  69. $options->imageBase64 = false;
  70. $options->logoSpaceWidth = 13;
  71. $options->logoSpaceHeight = 13;
  72. $options->scale = 5;
  73. $options->imageTransparent = false;
  74. header('Content-type: image/png');
  75. $qrOutputInterface = new QRImageWithLogo($options, (new QRCode($options))->getMatrix('https://www.youtube.com/watch?v=dQw4w9WgXcQ'));
  76. // dump the output, with an additional logo
  77. echo $qrOutputInterface->dump(null, __DIR__.'/octocat.png');