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