imageWithRoundedShapes.php 4.3 KB

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