QRImageWithText.php 2.7 KB

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