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