File indexing completed on 2025-03-02 05:33:39
0001 <?php 0002 0003 namespace Intervention\Image\Imagick\Commands; 0004 0005 use Intervention\Image\Imagick\Color; 0006 0007 class TrimCommand extends \Intervention\Image\Commands\AbstractCommand 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 $checkTransparency = false; 0026 0027 // define borders to trim away 0028 if (is_null($away)) { 0029 $away = ['top', 'right', 'bottom', 'left']; 0030 } elseif (is_string($away)) { 0031 $away = [$away]; 0032 } 0033 0034 // lower border names 0035 foreach ($away as $key => $value) { 0036 $away[$key] = strtolower($value); 0037 } 0038 0039 // define base color position 0040 switch (strtolower($base)) { 0041 case 'transparent': 0042 case 'trans': 0043 $checkTransparency = true; 0044 $base_x = 0; 0045 $base_y = 0; 0046 break; 0047 0048 case 'bottom-right': 0049 case 'right-bottom': 0050 $base_x = $width - 1; 0051 $base_y = $height - 1; 0052 break; 0053 0054 default: 0055 case 'top-left': 0056 case 'left-top': 0057 $base_x = 0; 0058 $base_y = 0; 0059 break; 0060 } 0061 0062 // pick base color 0063 if ($checkTransparency) { 0064 $base_color = new Color; // color will only be used to compare alpha channel 0065 } else { 0066 $base_color = $image->pickColor($base_x, $base_y, 'object'); 0067 } 0068 0069 // trim on clone to get only coordinates 0070 $trimed = clone $image->getCore(); 0071 0072 // add border to trim specific color 0073 $trimed->borderImage($base_color->getPixel(), 1, 1); 0074 0075 // trim image 0076 $trimed->trimImage(65850 / 100 * $tolerance); 0077 0078 // get coordinates of trim 0079 $imagePage = $trimed->getImagePage(); 0080 list($crop_x, $crop_y) = [$imagePage['x']-1, $imagePage['y']-1]; 0081 // $trimed->setImagePage(0, 0, 0, 0); 0082 list($crop_width, $crop_height) = [$trimed->width, $trimed->height]; 0083 0084 // adjust settings if right should not be trimed 0085 if ( ! in_array('right', $away)) { 0086 $crop_width = $crop_width + ($width - ($width - $crop_x)); 0087 } 0088 0089 // adjust settings if bottom should not be trimed 0090 if ( ! in_array('bottom', $away)) { 0091 $crop_height = $crop_height + ($height - ($height - $crop_y)); 0092 } 0093 0094 // adjust settings if left should not be trimed 0095 if ( ! in_array('left', $away)) { 0096 $crop_width = $crop_width + $crop_x; 0097 $crop_x = 0; 0098 } 0099 0100 // adjust settings if top should not be trimed 0101 if ( ! in_array('top', $away)) { 0102 $crop_height = $crop_height + $crop_y; 0103 $crop_y = 0; 0104 } 0105 0106 // add feather 0107 $crop_width = min($width, ($crop_width + $feather * 2)); 0108 $crop_height = min($height, ($crop_height + $feather * 2)); 0109 $crop_x = max(0, ($crop_x - $feather)); 0110 $crop_y = max(0, ($crop_y - $feather)); 0111 0112 // finally crop based on page 0113 $image->getCore()->cropImage($crop_width, $crop_height, $crop_x, $crop_y); 0114 $image->getCore()->setImagePage(0,0,0,0); 0115 0116 $trimed->destroy(); 0117 0118 return true; 0119 } 0120 }