imageWithRoundedShapes.php 4.2 KB

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