imageWithRoundedShapes.php 4.3 KB

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