QRImageWithText.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 base64_encode, 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. $this->image = imagecreatetruecolor($this->length, $this->length);
  31. $background = imagecolorallocate($this->image, ...$this->options->imageTransparencyBG);
  32. if((bool)$this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
  33. imagecolortransparent($this->image, $background);
  34. }
  35. imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $background);
  36. foreach($this->matrix->matrix() as $y => $row){
  37. foreach($row as $x => $M_TYPE){
  38. $this->setPixel($x, $y, $this->moduleValues[$M_TYPE]);
  39. }
  40. }
  41. // render text output if a string is given
  42. if($text !== null){
  43. $this->addText($text);
  44. }
  45. $imageData = $this->dumpImage($file);
  46. if((bool)$this->options->imageBase64){
  47. $imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
  48. }
  49. return $imageData;
  50. }
  51. /**
  52. * @param string $text
  53. */
  54. protected function addText(string $text):void{
  55. // save the qrcode image
  56. $qrcode = $this->image;
  57. // options things
  58. $textSize = 3; // see imagefontheight() and imagefontwidth()
  59. $textBG = [200, 200, 200];
  60. $textColor = [50, 50, 50];
  61. $bgWidth = $this->length;
  62. $bgHeight = $bgWidth + 20; // 20px extra space
  63. // create a new image with additional space
  64. $this->image = imagecreatetruecolor($bgWidth, $bgHeight);
  65. $background = imagecolorallocate($this->image, ...$textBG);
  66. // allow transparency
  67. if((bool)$this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
  68. imagecolortransparent($this->image, $background);
  69. }
  70. // fill the background
  71. imagefilledrectangle($this->image, 0, 0, $bgWidth, $bgHeight, $background);
  72. // copy over the qrcode
  73. imagecopymerge($this->image, $qrcode, 0, 0, 0, 0, $this->length, $this->length, 100);
  74. imagedestroy($qrcode);
  75. $fontColor = imagecolorallocate($this->image, ...$textColor);
  76. $w = imagefontwidth($textSize);
  77. $x = round(($bgWidth - strlen($text) * $w) / 2);
  78. // loop through the string and draw the letters
  79. foreach(str_split($text) as $i => $chr){
  80. imagechar($this->image, $textSize, $i * $w + $x, $this->length, $chr, $fontColor);
  81. }
  82. }
  83. }