Cheetah
TrimCommand.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use \Intervention\Image\Imagick\Color;
6 
8 {
15  public function execute($image)
16  {
17  $base = $this->argument(0)->type('string')->value();
18  $away = $this->argument(1)->value();
19  $tolerance = $this->argument(2)->type('numeric')->value(0);
20  $feather = $this->argument(3)->type('numeric')->value(0);
21 
22  $width = $image->getWidth();
23  $height = $image->getHeight();
24 
25  $checkTransparency = false;
26 
27  // define borders to trim away
28  if (is_null($away)) {
29  $away = array('top', 'right', 'bottom', 'left');
30  } elseif (is_string($away)) {
31  $away = array($away);
32  }
33 
34  // lower border names
35  foreach ($away as $key => $value) {
36  $away[$key] = strtolower($value);
37  }
38 
39  // define base color position
40  switch (strtolower($base)) {
41  case 'transparent':
42  case 'trans':
43  $checkTransparency = true;
44  $base_x = 0;
45  $base_y = 0;
46  break;
47 
48  case 'bottom-right':
49  case 'right-bottom':
50  $base_x = $width - 1;
51  $base_y = $height - 1;
52  break;
53 
54  default:
55  case 'top-left':
56  case 'left-top':
57  $base_x = 0;
58  $base_y = 0;
59  break;
60  }
61 
62  // pick base color
63  if ($checkTransparency) {
64  $base_color = new Color; // color will only be used to compare alpha channel
65  } else {
66  $base_color = $image->pickColor($base_x, $base_y, 'object');
67  }
68 
69  // trim on clone to get only coordinates
70  $trimed = clone $image->getCore();
71 
72  // add border to trim specific color
73  $trimed->borderImage($base_color->getPixel(), 1, 1);
74 
75  // trim image
76  $trimed->trimImage(65850 / 100 * $tolerance);
77 
78  // get coordinates of trim
79  $imagePage = $trimed->getImagePage();
80  list($crop_x, $crop_y) = array($imagePage['x']-1, $imagePage['y']-1);
81  // $trimed->setImagePage(0, 0, 0, 0);
82  list($crop_width, $crop_height) = array($trimed->width, $trimed->height);
83 
84  // adjust settings if right should not be trimed
85  if ( ! in_array('right', $away)) {
86  $crop_width = $crop_width + ($width - ($width - $crop_x));
87  }
88 
89  // adjust settings if bottom should not be trimed
90  if ( ! in_array('bottom', $away)) {
91  $crop_height = $crop_height + ($height - ($height - $crop_y));
92  }
93 
94  // adjust settings if left should not be trimed
95  if ( ! in_array('left', $away)) {
96  $crop_width = $crop_width + $crop_x;
97  $crop_x = 0;
98  }
99 
100  // adjust settings if top should not be trimed
101  if ( ! in_array('top', $away)) {
102  $crop_height = $crop_height + $crop_y;
103  $crop_y = 0;
104  }
105 
106  // add feather
107  $crop_width = min($width, ($crop_width + $feather * 2));
108  $crop_height = min($height, ($crop_height + $feather * 2));
109  $crop_x = max(0, ($crop_x - $feather));
110  $crop_y = max(0, ($crop_y - $feather));
111 
112  // finally crop based on page
113  $image->getCore()->cropImage($crop_width, $crop_height, $crop_x, $crop_y);
114  $image->getCore()->setImagePage(0,0,0,0);
115 
116  $trimed->destroy();
117 
118  return true;
119  }
120 }
Intervention\Image\Imagick\Color
Definition: Color.php:6
php
Intervention\Image\Imagick\Commands
Definition: BackupCommand.php:3
Intervention\Image\Imagick\Commands\TrimCommand
Definition: TrimCommand.php:8
Intervention\Image\Commands\AbstractCommand\argument
argument($key)
Definition: AbstractCommand.php:45
Intervention\Image\Commands\AbstractCommand
Definition: AbstractCommand.php:6
Intervention\Image\Imagick\Commands\TrimCommand\execute
execute($image)
Definition: TrimCommand.php:15
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10