控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GDLuminanceSource.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Zxing;
  3. /**
  4. * This class is used to help decode images from files which arrive as GD Resource
  5. * It does not support rotation.
  6. *
  7. *
  8. *
  9. */
  10. final class GDLuminanceSource extends LuminanceSource
  11. {
  12. public $luminances;
  13. private $dataWidth;
  14. private $dataHeight;
  15. /**
  16. * @var mixed|int
  17. */
  18. private $left;
  19. /**
  20. * @var mixed|int
  21. */
  22. private $top;
  23. /**
  24. * @var mixed|null
  25. */
  26. private $gdImage;
  27. public function __construct(
  28. $gdImage,
  29. $dataWidth,
  30. $dataHeight,
  31. $left = null,
  32. $top = null,
  33. $width = null,
  34. $height = null
  35. ) {
  36. if (!$left && !$top && !$width && !$height) {
  37. $this->GDLuminanceSource($gdImage, $dataWidth, $dataHeight);
  38. return;
  39. }
  40. parent::__construct($width, $height);
  41. if ($left + $width > $dataWidth || $top + $height > $dataHeight) {
  42. throw new \InvalidArgumentException("Crop rectangle does not fit within image data.");
  43. }
  44. $this->luminances = $gdImage;
  45. $this->dataWidth = $dataWidth;
  46. $this->dataHeight = $dataHeight;
  47. $this->left = $left;
  48. $this->top = $top;
  49. }
  50. public function GDLuminanceSource($gdImage, $width, $height): void
  51. {
  52. parent::__construct($width, $height);
  53. $this->dataWidth = $width;
  54. $this->dataHeight = $height;
  55. $this->left = 0;
  56. $this->top = 0;
  57. $this->gdImage = $gdImage;
  58. // In order to measure pure decoding speed, we convert the entire image to a greyscale array
  59. // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.
  60. $this->luminances = [];
  61. //$this->luminances = $this->grayScaleToBitmap($this->grayscale());
  62. $array = [];
  63. $rgb = [];
  64. for ($j = 0; $j < $height; $j++) {
  65. for ($i = 0; $i < $width; $i++) {
  66. $argb = imagecolorat($this->gdImage, $i, $j);
  67. $pixel = imagecolorsforindex($this->gdImage, $argb);
  68. $r = $pixel['red'];
  69. $g = $pixel['green'];
  70. $b = $pixel['blue'];
  71. if ($r == $g && $g == $b) {
  72. // Image is already greyscale, so pick any channel.
  73. $this->luminances[] = $r;//(($r + 128) % 256) - 128;
  74. } else {
  75. // Calculate luminance cheaply, favoring green.
  76. $this->luminances[] = ($r + 2 * $g + $b) / 4;//(((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
  77. }
  78. }
  79. }
  80. /*
  81. for ($y = 0; $y < $height; $y++) {
  82. $offset = $y * $width;
  83. for ($x = 0; $x < $width; $x++) {
  84. $pixel = $pixels[$offset + $x];
  85. $r = ($pixel >> 16) & 0xff;
  86. $g = ($pixel >> 8) & 0xff;
  87. $b = $pixel & 0xff;
  88. if ($r == $g && $g == $b) {
  89. // Image is already greyscale, so pick any channel.
  90. $this->luminances[(int)($offset + $x)] = (($r+128) % 256) - 128;
  91. } else {
  92. // Calculate luminance cheaply, favoring green.
  93. $this->luminances[(int)($offset + $x)] = (((($r + 2 * $g + $b) / 4)+128)%256) - 128;
  94. }
  95. }
  96. */
  97. //}
  98. // $this->luminances = $this->grayScaleToBitmap($this->luminances);
  99. }
  100. //@Override
  101. public function getRow($y, $row = null)
  102. {
  103. if ($y < 0 || $y >= $this->getHeight()) {
  104. throw new \InvalidArgumentException('Requested row is outside the image: ' . $y);
  105. }
  106. $width = $this->getWidth();
  107. if ($row == null || (is_countable($row) ? count($row) : 0) < $width) {
  108. $row = [];
  109. }
  110. $offset = ($y + $this->top) * $this->dataWidth + $this->left;
  111. $row = arraycopy($this->luminances, $offset, $row, 0, $width);
  112. return $row;
  113. }
  114. //@Override
  115. public function getMatrix()
  116. {
  117. $width = $this->getWidth();
  118. $height = $this->getHeight();
  119. // If the caller asks for the entire underlying image, save the copy and give them the
  120. // original data. The docs specifically warn that result.length must be ignored.
  121. if ($width == $this->dataWidth && $height == $this->dataHeight) {
  122. return $this->luminances;
  123. }
  124. $area = $width * $height;
  125. $matrix = [];
  126. $inputOffset = $this->top * $this->dataWidth + $this->left;
  127. // If the width matches the full width of the underlying data, perform a single copy.
  128. if ($width == $this->dataWidth) {
  129. $matrix = arraycopy($this->luminances, $inputOffset, $matrix, 0, $area);
  130. return $matrix;
  131. }
  132. // Otherwise copy one cropped row at a time.
  133. $rgb = $this->luminances;
  134. for ($y = 0; $y < $height; $y++) {
  135. $outputOffset = $y * $width;
  136. $matrix = arraycopy($rgb, $inputOffset, $matrix, $outputOffset, $width);
  137. $inputOffset += $this->dataWidth;
  138. }
  139. return $matrix;
  140. }
  141. //@Override
  142. public function isCropSupported()
  143. {
  144. return true;
  145. }
  146. //@Override
  147. public function crop($left, $top, $width, $height): \Zxing\GDLuminanceSource
  148. {
  149. return new GDLuminanceSource(
  150. $this->luminances,
  151. $this->dataWidth,
  152. $this->dataHeight,
  153. $this->left + $left,
  154. $this->top + $top,
  155. $width,
  156. $height
  157. );
  158. }
  159. }