TrimCommand.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Intervention\Image\Gd\Commands;
  3. use Intervention\Image\Gd\Color;
  4. class TrimCommand extends ResizeCommand
  5. {
  6. /**
  7. * Trims away parts of an image
  8. *
  9. * @param \Intervention\Image\Image $image
  10. * @return boolean
  11. */
  12. public function execute($image)
  13. {
  14. $base = $this->argument(0)->type('string')->value();
  15. $away = $this->argument(1)->value();
  16. $tolerance = $this->argument(2)->type('numeric')->value(0);
  17. $feather = $this->argument(3)->type('numeric')->value(0);
  18. $width = $image->getWidth();
  19. $height = $image->getHeight();
  20. // default values
  21. $checkTransparency = false;
  22. // define borders to trim away
  23. if (is_null($away)) {
  24. $away = ['top', 'right', 'bottom', 'left'];
  25. } elseif (is_string($away)) {
  26. $away = [$away];
  27. }
  28. // lower border names
  29. foreach ($away as $key => $value) {
  30. $away[$key] = strtolower($value);
  31. }
  32. // define base color position
  33. switch (strtolower($base)) {
  34. case 'transparent':
  35. case 'trans':
  36. $checkTransparency = true;
  37. $base_x = 0;
  38. $base_y = 0;
  39. break;
  40. case 'bottom-right':
  41. case 'right-bottom':
  42. $base_x = $width - 1;
  43. $base_y = $height - 1;
  44. break;
  45. default:
  46. case 'top-left':
  47. case 'left-top':
  48. $base_x = 0;
  49. $base_y = 0;
  50. break;
  51. }
  52. // pick base color
  53. if ($checkTransparency) {
  54. $color = new Color; // color will only be used to compare alpha channel
  55. } else {
  56. $color = $image->pickColor($base_x, $base_y, 'object');
  57. }
  58. $top_x = 0;
  59. $top_y = 0;
  60. $bottom_x = $width;
  61. $bottom_y = $height;
  62. // search upper part of image for colors to trim away
  63. if (in_array('top', $away)) {
  64. for ($y=0; $y < ceil($height/2); $y++) {
  65. for ($x=0; $x < $width; $x++) {
  66. $checkColor = $image->pickColor($x, $y, 'object');
  67. if ($checkTransparency) {
  68. $checkColor->r = $color->r;
  69. $checkColor->g = $color->g;
  70. $checkColor->b = $color->b;
  71. }
  72. if ($color->differs($checkColor, $tolerance)) {
  73. $top_y = max(0, $y - $feather);
  74. break 2;
  75. }
  76. }
  77. }
  78. }
  79. // search left part of image for colors to trim away
  80. if (in_array('left', $away)) {
  81. for ($x=0; $x < ceil($width/2); $x++) {
  82. for ($y=$top_y; $y < $height; $y++) {
  83. $checkColor = $image->pickColor($x, $y, 'object');
  84. if ($checkTransparency) {
  85. $checkColor->r = $color->r;
  86. $checkColor->g = $color->g;
  87. $checkColor->b = $color->b;
  88. }
  89. if ($color->differs($checkColor, $tolerance)) {
  90. $top_x = max(0, $x - $feather);
  91. break 2;
  92. }
  93. }
  94. }
  95. }
  96. // search lower part of image for colors to trim away
  97. if (in_array('bottom', $away)) {
  98. for ($y=($height-1); $y >= floor($height/2)-1; $y--) {
  99. for ($x=$top_x; $x < $width; $x++) {
  100. $checkColor = $image->pickColor($x, $y, 'object');
  101. if ($checkTransparency) {
  102. $checkColor->r = $color->r;
  103. $checkColor->g = $color->g;
  104. $checkColor->b = $color->b;
  105. }
  106. if ($color->differs($checkColor, $tolerance)) {
  107. $bottom_y = min($height, $y+1 + $feather);
  108. break 2;
  109. }
  110. }
  111. }
  112. }
  113. // search right part of image for colors to trim away
  114. if (in_array('right', $away)) {
  115. for ($x=($width-1); $x >= floor($width/2)-1; $x--) {
  116. for ($y=$top_y; $y < $bottom_y; $y++) {
  117. $checkColor = $image->pickColor($x, $y, 'object');
  118. if ($checkTransparency) {
  119. $checkColor->r = $color->r;
  120. $checkColor->g = $color->g;
  121. $checkColor->b = $color->b;
  122. }
  123. if ($color->differs($checkColor, $tolerance)) {
  124. $bottom_x = min($width, $x+1 + $feather);
  125. break 2;
  126. }
  127. }
  128. }
  129. }
  130. // trim parts of image
  131. return $this->modify($image, 0, 0, $top_x, $top_y, ($bottom_x-$top_x), ($bottom_y-$top_y), ($bottom_x-$top_x), ($bottom_y-$top_y));
  132. }
  133. }