QRImageWithText.php 2.9 KB

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