Cheetah
MaskCommand.php
Go to the documentation of this file.
1 <?php
2 
4 
6 {
13  public function execute($image)
14  {
15  $mask_source = $this->argument(0)->value();
16  $mask_w_alpha = $this->argument(1)->type('bool')->value(false);
17 
18  // get imagick
19  $imagick = $image->getCore();
20 
21  // build mask image from source
22  $mask = $image->getDriver()->init($mask_source);
23 
24  // resize mask to size of current image (if necessary)
25  $image_size = $image->getSize();
26  if ($mask->getSize() != $image_size) {
27  $mask->resize($image_size->width, $image_size->height);
28  }
29 
30  $imagick->setImageMatte(true);
31 
32  if ($mask_w_alpha) {
33 
34  // just mask with alpha map
35  $imagick->compositeImage($mask->getCore(), \Imagick::COMPOSITE_DSTIN, 0, 0);
36 
37  } else {
38 
39  // get alpha channel of original as greyscale image
40  $original_alpha = clone $imagick;
41  $original_alpha->separateImageChannel(\Imagick::CHANNEL_ALPHA);
42 
43  // use red channel from mask ask alpha
44  $mask_alpha = clone $mask->getCore();
45  $mask_alpha->compositeImage($mask->getCore(), \Imagick::COMPOSITE_DEFAULT, 0, 0);
46  // $mask_alpha->setImageAlphaChannel(\Imagick::ALPHACHANNEL_DEACTIVATE);
47  $mask_alpha->separateImageChannel(\Imagick::CHANNEL_ALL);
48 
49  // combine both alphas from original and mask
50  $original_alpha->compositeImage($mask_alpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
51 
52  // mask the image with the alpha combination
53  $imagick->compositeImage($original_alpha, \Imagick::COMPOSITE_DSTIN, 0, 0);
54  }
55 
56  return true;
57  }
58 }
Intervention\Image\Imagick\Commands\MaskCommand
Definition: MaskCommand.php:6
Intervention\Image\Imagick\Commands\MaskCommand\execute
execute($image)
Definition: MaskCommand.php:13
php
Intervention\Image\Imagick\Commands
Definition: BackupCommand.php:3
Intervention\Image\Commands\AbstractCommand\argument
argument($key)
Definition: AbstractCommand.php:45
Intervention\Image\Commands\AbstractCommand
Definition: AbstractCommand.php:6