imageWithRoundedShapes.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * php-gd realization of QR code with rounded modules
  4. *
  5. * @see https://github.com/chillerlan/php-qrcode/pull/215
  6. * @see https://github.com/chillerlan/php-qrcode/issues/127
  7. *
  8. * @created 17.09.2023
  9. * @author livingroot
  10. * @copyright 2023 livingroot
  11. * @license MIT
  12. *
  13. * @noinspection PhpComposerExtensionStubsInspection
  14. */
  15. declare(strict_types=1);
  16. use chillerlan\QRCode\{QRCode, QROptions};
  17. use chillerlan\QRCode\Common\EccLevel;
  18. use chillerlan\QRCode\Data\QRMatrix;
  19. use chillerlan\QRCode\Output\QRGdImagePNG;
  20. use chillerlan\Settings\SettingsContainerInterface;
  21. require_once __DIR__.'/../vendor/autoload.php';
  22. // --------------------
  23. // Class definition
  24. // --------------------
  25. class QRGdRounded extends QRGdImagePNG{
  26. public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){
  27. // enable the internal scaling for better rounding results at scale < 20
  28. $options->drawCircularModules = true;
  29. parent::__construct($options, $matrix);
  30. }
  31. protected function module(int $x, int $y, int $M_TYPE):void{
  32. /**
  33. * The bit order (starting from 0):
  34. *
  35. * 0 1 2
  36. * 7 # 3
  37. * 6 5 4
  38. */
  39. $neighbours = $this->matrix->checkNeighbours($x, $y);
  40. $x1 = ($x * $this->scale);
  41. $y1 = ($y * $this->scale);
  42. $x2 = (($x + 1) * $this->scale);
  43. $y2 = (($y + 1) * $this->scale);
  44. $rectsize = (int)($this->scale / 2);
  45. $light = $this->getModuleValue($M_TYPE);
  46. $dark = $this->getModuleValue($M_TYPE | QRMatrix::IS_DARK);
  47. // ------------------
  48. // Outer rounding
  49. // ------------------
  50. if(($neighbours & (1 << 7))){ // neighbour left
  51. // top left
  52. imagefilledrectangle($this->image, $x1, $y1, ($x1 + $rectsize), ($y1 + $rectsize), $light);
  53. // bottom left
  54. imagefilledrectangle($this->image, $x1, ($y2 - $rectsize), ($x1 + $rectsize), $y2, $light);
  55. }
  56. if(($neighbours & (1 << 3))){ // neighbour right
  57. // top right
  58. imagefilledrectangle($this->image, ($x2 - $rectsize), $y1, $x2, ($y1 + $rectsize), $light);
  59. // bottom right
  60. imagefilledrectangle($this->image, ($x2 - $rectsize), ($y2 - $rectsize), $x2, $y2, $light);
  61. }
  62. if(($neighbours & (1 << 1))){ // neighbour top
  63. // top left
  64. imagefilledrectangle($this->image, $x1, $y1, ($x1 + $rectsize), ($y1 + $rectsize), $light);
  65. // top right
  66. imagefilledrectangle($this->image, ($x2 - $rectsize), $y1, $x2, ($y1 + $rectsize), $light);
  67. }
  68. if(($neighbours & (1 << 5))){ // neighbour bottom
  69. // bottom left
  70. imagefilledrectangle($this->image, $x1, ($y2 - $rectsize), ($x1 + $rectsize), $y2, $light);
  71. // bottom right
  72. imagefilledrectangle($this->image, ($x2 - $rectsize), ($y2 - $rectsize), $x2, $y2, $light);
  73. }
  74. // ---------------------
  75. // inner rounding
  76. // ---------------------
  77. if(!$this->matrix->check($x, $y)){
  78. if(($neighbours & 1) && ($neighbours & (1 << 7)) && ($neighbours & (1 << 1))){
  79. // top left
  80. imagefilledrectangle($this->image, $x1, $y1, ($x1 + $rectsize), ($y1 + $rectsize), $dark);
  81. }
  82. if(($neighbours & (1 << 1)) && ($neighbours & (1 << 2)) && ($neighbours & (1 << 3))){
  83. // top right
  84. imagefilledrectangle($this->image, ($x2 - $rectsize), $y1, $x2, ($y1 + $rectsize), $dark);
  85. }
  86. if(($neighbours & (1 << 7)) && ($neighbours & (1 << 6)) && ($neighbours & (1 << 5))){
  87. // bottom left
  88. imagefilledrectangle($this->image, $x1, ($y2 - $rectsize), ($x1 + $rectsize), $y2, $dark);
  89. }
  90. if(($neighbours & (1 << 3)) && ($neighbours & (1 << 4)) && ($neighbours & (1 << 5))){
  91. // bottom right
  92. imagefilledrectangle($this->image, ($x2 - $rectsize), ($y2 - $rectsize), $x2, $y2, $dark);
  93. }
  94. }
  95. imagefilledellipse(
  96. $this->image,
  97. (int)($x * $this->scale + $this->scale / 2),
  98. (int)($y * $this->scale + $this->scale / 2),
  99. ($this->scale - 1),
  100. ($this->scale - 1),
  101. $light,
  102. );
  103. }
  104. }
  105. // --------------------
  106. // Example
  107. // --------------------
  108. $options = new QROptions([
  109. 'version' => 7,
  110. 'eccLevel' => EccLevel::H,
  111. 'outputInterface' => QRGdRounded::class,
  112. 'outputBase64' => false,
  113. 'scale' => 30,
  114. 'addLogoSpace' => true,
  115. 'logoSpaceWidth' => 13,
  116. 'logoSpaceHeight' => 13,
  117. ]);
  118. $img = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  119. header('Content-type: image/png');
  120. echo $img;