imageWithRoundedShapes.php 4.4 KB

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