控制台应用,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.

IMagickLuminanceSource.php 4.0KB

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