imageWithText.php 2.9 KB

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