File indexing completed on 2025-01-26 05:29:14

0001 <?php
0002 
0003 namespace Intervention\Image\Gd\Commands;
0004 
0005 use Intervention\Image\Gd\Color;
0006 
0007 class TrimCommand extends ResizeCommand
0008 {
0009     /**
0010      * Trims away parts of an image
0011      *
0012      * @param  \Intervention\Image\Image $image
0013      * @return boolean
0014      */
0015     public function execute($image)
0016     {
0017         $base = $this->argument(0)->type('string')->value();
0018         $away = $this->argument(1)->value();
0019         $tolerance = $this->argument(2)->type('numeric')->value(0);
0020         $feather = $this->argument(3)->type('numeric')->value(0);
0021 
0022         $width = $image->getWidth();
0023         $height = $image->getHeight();
0024 
0025         // default values
0026         $checkTransparency = false;
0027 
0028         // define borders to trim away
0029         if (is_null($away)) {
0030             $away = ['top', 'right', 'bottom', 'left'];
0031         } elseif (is_string($away)) {
0032             $away = [$away];
0033         }
0034 
0035         // lower border names
0036         foreach ($away as $key => $value) {
0037             $away[$key] = strtolower($value);
0038         }
0039 
0040         // define base color position
0041         switch (strtolower($base)) {
0042             case 'transparent':
0043             case 'trans':
0044                 $checkTransparency = true;
0045                 $base_x = 0;
0046                 $base_y = 0;
0047                 break;
0048 
0049             case 'bottom-right':
0050             case 'right-bottom':
0051                 $base_x = $width - 1;
0052                 $base_y = $height - 1;
0053                 break;
0054 
0055             default:
0056             case 'top-left':
0057             case 'left-top':
0058                 $base_x = 0;
0059                 $base_y = 0;
0060                 break;
0061         }
0062 
0063         // pick base color
0064         if ($checkTransparency) {
0065             $color = new Color; // color will only be used to compare alpha channel
0066         } else {
0067             $color = $image->pickColor($base_x, $base_y, 'object');
0068         }
0069 
0070         $top_x = 0;
0071         $top_y = 0;
0072         $bottom_x = $width;
0073         $bottom_y = $height;
0074 
0075         // search upper part of image for colors to trim away
0076         if (in_array('top', $away)) {
0077 
0078             for ($y=0; $y < ceil($height/2); $y++) {
0079                 for ($x=0; $x < $width; $x++) {
0080 
0081                     $checkColor = $image->pickColor($x, $y, 'object');
0082 
0083                     if ($checkTransparency) {
0084                         $checkColor->r = $color->r;
0085                         $checkColor->g = $color->g;
0086                         $checkColor->b = $color->b;
0087                     }
0088 
0089                     if ($color->differs($checkColor, $tolerance)) {
0090                         $top_y = max(0, $y - $feather);
0091                         break 2;
0092                     }
0093 
0094                 }
0095             }
0096 
0097         }
0098 
0099         // search left part of image for colors to trim away
0100         if (in_array('left', $away)) {
0101 
0102             for ($x=0; $x < ceil($width/2); $x++) {
0103                 for ($y=$top_y; $y < $height; $y++) {
0104 
0105                     $checkColor = $image->pickColor($x, $y, 'object');
0106 
0107                     if ($checkTransparency) {
0108                         $checkColor->r = $color->r;
0109                         $checkColor->g = $color->g;
0110                         $checkColor->b = $color->b;
0111                     }
0112 
0113                     if ($color->differs($checkColor, $tolerance)) {
0114                         $top_x = max(0, $x - $feather);
0115                         break 2;
0116                     }
0117 
0118                 }
0119             }
0120 
0121         }
0122 
0123         // search lower part of image for colors to trim away
0124         if (in_array('bottom', $away)) {
0125 
0126             for ($y=($height-1); $y >= floor($height/2)-1; $y--) {
0127                 for ($x=$top_x; $x < $width; $x++) {
0128 
0129                     $checkColor = $image->pickColor($x, $y, 'object');
0130 
0131                     if ($checkTransparency) {
0132                         $checkColor->r = $color->r;
0133                         $checkColor->g = $color->g;
0134                         $checkColor->b = $color->b;
0135                     }
0136 
0137                     if ($color->differs($checkColor, $tolerance)) {
0138                         $bottom_y = min($height, $y+1 + $feather);
0139                         break 2;
0140                     }
0141 
0142                 }
0143             }
0144 
0145         }
0146 
0147         // search right part of image for colors to trim away
0148         if (in_array('right', $away)) {
0149 
0150             for ($x=($width-1); $x >= floor($width/2)-1; $x--) {
0151                 for ($y=$top_y; $y < $bottom_y; $y++) {
0152 
0153                     $checkColor = $image->pickColor($x, $y, 'object');
0154 
0155                     if ($checkTransparency) {
0156                         $checkColor->r = $color->r;
0157                         $checkColor->g = $color->g;
0158                         $checkColor->b = $color->b;
0159                     }
0160 
0161                     if ($color->differs($checkColor, $tolerance)) {
0162                         $bottom_x = min($width, $x+1 + $feather);
0163                         break 2;
0164                     }
0165 
0166                 }
0167             }
0168 
0169         }
0170 
0171 
0172         // trim parts of image
0173         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));
0174 
0175     }
0176 }