imageWithText.php 3.0 KB

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