QRImageWithText.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Class QRImageWithText
  4. *
  5. * example for additional text
  6. *
  7. * @link https://github.com/chillerlan/php-qrcode/issues/35
  8. *
  9. * @created 22.06.2019
  10. * @author smiley <smiley@chillerlan.net>
  11. * @copyright 2019 smiley
  12. * @license MIT
  13. *
  14. * @noinspection PhpComposerExtensionStubsInspection
  15. */
  16. namespace chillerlan\QRCodeExamples;
  17. use chillerlan\QRCode\Output\QRImage;
  18. use function imagechar, imagecolorallocate, imagecolortransparent, imagecopymerge, imagecreatetruecolor,
  19. imagedestroy, imagefilledrectangle, imagefontwidth, in_array, round, str_split, strlen;
  20. class QRImageWithText extends QRImage{
  21. /**
  22. * @param string|null $file
  23. * @param string|null $text
  24. *
  25. * @return string
  26. */
  27. public function dump(string $file = null, string $text = null):string{
  28. // set returnResource to true to skip further processing for now
  29. $this->options->returnResource = true;
  30. // there's no need to save the result of dump() into $this->image here
  31. parent::dump($file);
  32. // render text output if a string is given
  33. if($text !== null){
  34. $this->addText($text);
  35. }
  36. $imageData = $this->dumpImage();
  37. if($file !== null){
  38. $this->saveToFile($imageData, $file);
  39. }
  40. if($this->options->imageBase64){
  41. $imageData = $this->base64encode($imageData, 'image/'.$this->options->outputType);
  42. }
  43. return $imageData;
  44. }
  45. /**
  46. * @param string $text
  47. */
  48. protected function addText(string $text):void{
  49. // save the qrcode image
  50. $qrcode = $this->image;
  51. // options things
  52. $textSize = 3; // see imagefontheight() and imagefontwidth()
  53. $textBG = [200, 200, 200];
  54. $textColor = [50, 50, 50];
  55. $bgWidth = $this->length;
  56. $bgHeight = $bgWidth + 20; // 20px extra space
  57. // create a new image with additional space
  58. $this->image = imagecreatetruecolor($bgWidth, $bgHeight);
  59. $background = imagecolorallocate($this->image, ...$textBG);
  60. // allow transparency
  61. if($this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
  62. imagecolortransparent($this->image, $background);
  63. }
  64. // fill the background
  65. imagefilledrectangle($this->image, 0, 0, $bgWidth, $bgHeight, $background);
  66. // copy over the qrcode
  67. imagecopymerge($this->image, $qrcode, 0, 0, 0, 0, $this->length, $this->length, 100);
  68. imagedestroy($qrcode);
  69. $fontColor = imagecolorallocate($this->image, ...$textColor);
  70. $w = imagefontwidth($textSize);
  71. $x = round(($bgWidth - strlen($text) * $w) / 2);
  72. // loop through the string and draw the letters
  73. foreach(str_split($text) as $i => $chr){
  74. imagechar($this->image, $textSize, (int)($i * $w + $x), $this->length, $chr, $fontColor);
  75. }
  76. }
  77. }