imageWithText.php 3.0 KB

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