wpfunnels
/
vendor
/
intervention
/
image
/
src
/
Intervention
/
Image
/
Gd
/
Commands
/
MaskCommand.php
MaskCommand.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.3, at vendor/intervention/image/src/Intervention/Image/Gd/Commands/MaskCommand.php
| 1 | <?php |
| 2 | |
| 3 | namespace Intervention\Image\Gd\Commands; |
| 4 | |
| 5 | use Intervention\Image\Commands\AbstractCommand; |
| 6 | |
| 7 | class MaskCommand extends AbstractCommand |
| 8 | { |
| 9 | /** |
| 10 | * Applies an alpha mask to an image |
| 11 | * |
| 12 | * @param \Intervention\Image\Image $image |
| 13 | * @return boolean |
| 14 | */ |
| 15 | public function execute($image) |
| 16 | { |
| 17 | $mask_source = $this->argument(0)->value(); |
| 18 | $mask_w_alpha = $this->argument(1)->type('bool')->value(false); |
| 19 | |
| 20 | $image_size = $image->getSize(); |
| 21 | |
| 22 | // create empty canvas |
| 23 | $canvas = $image->getDriver()->newImage($image_size->width, $image_size->height, [0,0,0,0]); |
| 24 | |
| 25 | // build mask image from source |
| 26 | $mask = $image->getDriver()->init($mask_source); |
| 27 | $mask_size = $mask->getSize(); |
| 28 | |
| 29 | // resize mask to size of current image (if necessary) |
| 30 | if ($mask_size != $image_size) { |
| 31 | $mask->resize($image_size->width, $image_size->height); |
| 32 | } |
| 33 | |
| 34 | imagealphablending($canvas->getCore(), false); |
| 35 | |
| 36 | if ( ! $mask_w_alpha) { |
| 37 | // mask from greyscale image |
| 38 | imagefilter($mask->getCore(), IMG_FILTER_GRAYSCALE); |
| 39 | } |
| 40 | |
| 41 | // redraw old image pixel by pixel considering alpha map |
| 42 | for ($x=0; $x < $image_size->width; $x++) { |
| 43 | for ($y=0; $y < $image_size->height; $y++) { |
| 44 | |
| 45 | $color = $image->pickColor($x, $y, 'array'); |
| 46 | $alpha = $mask->pickColor($x, $y, 'array'); |
| 47 | |
| 48 | if ($mask_w_alpha) { |
| 49 | $alpha = $alpha[3]; // use alpha channel as mask |
| 50 | } else { |
| 51 | |
| 52 | if ($alpha[3] == 0) { // transparent as black |
| 53 | $alpha = 0; |
| 54 | } else { |
| 55 | |
| 56 | // $alpha = floatval(round((($alpha[0] + $alpha[1] + $alpha[3]) / 3) / 255, 2)); |
| 57 | |
| 58 | // image is greyscale, so channel doesn't matter (use red channel) |
| 59 | $alpha = floatval(round($alpha[0] / 255, 2)); |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |
| 64 | // preserve alpha of original image... |
| 65 | if ($color[3] < $alpha) { |
| 66 | $alpha = $color[3]; |
| 67 | } |
| 68 | |
| 69 | // replace alpha value |
| 70 | $color[3] = $alpha; |
| 71 | |
| 72 | // redraw pixel |
| 73 | $canvas->pixel($color, $x, $y); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // replace current image with masked instance |
| 79 | $image->setCore($canvas->getCore()); |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 |