imageWithText.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\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. if($file !== null){
  34. $this->saveToFile($imageData, $file);
  35. }
  36. if($this->options->imageBase64){
  37. $imageData = $this->base64encode($imageData, 'image/'.$this->options->outputType);
  38. }
  39. return $imageData;
  40. }
  41. /**
  42. * @inheritDoc
  43. */
  44. protected function addText(string $text):void{
  45. // save the qrcode image
  46. $qrcode = $this->image;
  47. // options things
  48. $textSize = 3; // see imagefontheight() and imagefontwidth()
  49. $textBG = [200, 200, 200];
  50. $textColor = [50, 50, 50];
  51. $bgWidth = $this->length;
  52. $bgHeight = $bgWidth + 20; // 20px extra space
  53. // create a new image with additional space
  54. $this->image = imagecreatetruecolor($bgWidth, $bgHeight);
  55. $background = imagecolorallocate($this->image, ...$textBG);
  56. // allow transparency
  57. if($this->options->imageTransparent && $this->options->outputType !== QRCode::OUTPUT_IMAGE_JPG){
  58. imagecolortransparent($this->image, $background);
  59. }
  60. // fill the background
  61. imagefilledrectangle($this->image, 0, 0, $bgWidth, $bgHeight, $background);
  62. // copy over the qrcode
  63. imagecopymerge($this->image, $qrcode, 0, 0, 0, 0, $this->length, $this->length, 100);
  64. imagedestroy($qrcode);
  65. $fontColor = imagecolorallocate($this->image, ...$textColor);
  66. $w = imagefontwidth($textSize);
  67. $x = round(($bgWidth - strlen($text) * $w) / 2);
  68. // loop through the string and draw the letters
  69. foreach(str_split($text) as $i => $chr){
  70. imagechar($this->image, $textSize, (int)($i * $w + $x), $this->length, $chr, $fontColor);
  71. }
  72. }
  73. }
  74. /*
  75. * Runtime
  76. */
  77. $options = new QROptions([
  78. 'version' => 7,
  79. 'outputType' => QRCode::OUTPUT_IMAGE_PNG,
  80. 'scale' => 3,
  81. 'imageBase64' => false,
  82. ]);
  83. $qrcode = new QRCode($options);
  84. $qrcode->addByteSegment('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  85. header('Content-type: image/png');
  86. $qrOutputInterface = new QRImageWithText($options, $qrcode->getMatrix());
  87. // dump the output, with additional text
  88. // the text could also be supplied via the options, see the svgWithLogo example
  89. echo $qrOutputInterface->dump(null, 'example text');
  90. exit;