imageWithText.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * GdImage example for displaying additional text under the QR Code
  4. *
  5. * @link https://github.com/chillerlan/php-qrcode/issues/35
  6. *
  7. * @created 22.06.2019
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2019 Smiley
  10. * @license MIT
  11. *
  12. * @noinspection PhpIllegalPsrClassPathInspection, PhpComposerExtensionStubsInspection
  13. */
  14. declare(strict_types=1);
  15. use chillerlan\QRCode\{QRCode, QROptions};
  16. use chillerlan\QRCode\Output\QRGdImagePNG;
  17. require_once __DIR__.'/../vendor/autoload.php';
  18. /*
  19. * Class definition
  20. */
  21. class QRImageWithText extends QRGdImagePNG{
  22. public function dump(string|null $file = null, string|null $text = null):string{
  23. // set returnResource to true to skip further processing for now
  24. $this->options->returnResource = true;
  25. // there's no need to save the result of dump() into $this->image here
  26. parent::dump($file);
  27. // render text output if a string is given
  28. if($text !== null){
  29. $this->addText($text);
  30. }
  31. $imageData = $this->dumpImage();
  32. $this->saveToFile($imageData, $file);
  33. if($this->options->outputBase64){
  34. $imageData = $this->toBase64DataURI($imageData);
  35. }
  36. return $imageData;
  37. }
  38. protected function addText(string $text):void{
  39. // save the qrcode image
  40. $qrcode = $this->image;
  41. // options things
  42. $textSize = 3; // see imagefontheight() and imagefontwidth()
  43. $textBG = [200, 200, 200];
  44. $textColor = [50, 50, 50];
  45. $bgWidth = $this->length;
  46. $bgHeight = ($bgWidth + 20); // 20px extra space
  47. // create a new image with additional space
  48. $this->image = imagecreatetruecolor($bgWidth, $bgHeight);
  49. $background = imagecolorallocate($this->image, ...$textBG);
  50. // allow transparency
  51. if($this->options->imageTransparent){
  52. imagecolortransparent($this->image, $background);
  53. }
  54. // fill the background
  55. imagefilledrectangle($this->image, 0, 0, $bgWidth, $bgHeight, $background);
  56. // copy over the qrcode
  57. imagecopymerge($this->image, $qrcode, 0, 0, 0, 0, $this->length, $this->length, 100);
  58. imagedestroy($qrcode);
  59. $fontColor = imagecolorallocate($this->image, ...$textColor);
  60. $w = imagefontwidth($textSize);
  61. $x = round(($bgWidth - strlen($text) * $w) / 2);
  62. // loop through the string and draw the letters
  63. foreach(str_split($text) as $i => $chr){
  64. imagechar($this->image, $textSize, (int)($i * $w + $x), $this->length, $chr, $fontColor);
  65. }
  66. }
  67. }
  68. /*
  69. * Runtime
  70. */
  71. $options = new QROptions;
  72. $options->version = 7;
  73. $options->scale = 3;
  74. $options->outputBase64 = false;
  75. $qrcode = new QRCode($options);
  76. $qrcode->addByteSegment('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  77. // invoke the custom output interface manually
  78. $qrOutputInterface = new QRImageWithText($options, $qrcode->getQRMatrix());
  79. // dump the output, with additional text
  80. // the text could also be supplied via the options, see the svgWithLogo example
  81. $out = $qrOutputInterface->dump(null, 'example text');
  82. header('Content-type: image/png');
  83. echo $out;
  84. exit;