QRImageWithText.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. $this->image = \imagecreatetruecolor($this->length, $this->length);
  26. $background = \imagecolorallocate($this->image, ...$this->options->imageTransparencyBG);
  27. if((bool)$this->options->imageTransparent && \in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
  28. \imagecolortransparent($this->image, $background);
  29. }
  30. \imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $background);
  31. foreach($this->matrix->matrix() as $y => $row){
  32. foreach($row as $x => $M_TYPE){
  33. $this->setPixel($x, $y, $this->moduleValues[$M_TYPE]);
  34. }
  35. }
  36. // render text output if a string is given
  37. if($text !== null){
  38. $this->addText($text);
  39. }
  40. $imageData = $this->dumpImage($file);
  41. if((bool)$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((bool)$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, $i * $w + $x, $this->length, $chr, $fontColor);
  76. }
  77. }
  78. }