Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add apply alpha layer mask #34

Open
GoodLittleDev opened this issue Feb 4, 2014 · 0 comments
Open

Add apply alpha layer mask #34

GoodLittleDev opened this issue Feb 4, 2014 · 0 comments

Comments

@GoodLittleDev
Copy link

add this code to: ImageWorkshopLayer.php
too allow you to apply layer mask.
to use $layer_you_wish_to_apply_mask->applyalphamask($mask_layer);

/**
* Apply alpha layer mask.
* can be slower so best for images you don't need to remake for every user.
* @param layer $mask
*
* @author email goodlittledeveloper@gmail.com
*
*/

public function applyalphamask($mask){
    $layerWidth = $this->width;
    $layerHeight = $this->height;

    $masktemp = clone $mask;

    $masktemp->resizeInPixel($layerWidth, $layerHeight); // make $mask and $layer the same size.
    $masktemp->applyFilter(IMG_FILTER_GRAYSCALE); //converts to greyscale if not greyscale;
    $masktemp->applyFilter(IMG_FILTER_NEGATE); 

    $layerImg= $this->getImage();
    $maskImg = $masktemp->getImage();

    $imgtemp = imageCreateTrueColor($layerWidth,$layerHeight);

    imagealphablending($imgtemp, false);
    imagesavealpha($imgtemp, true);


    for ($h=0; $h<$layerHeight; $h++){
        for ($w=0; $w<$layerWidth; $w++){
            $Lrgb = imagecolorat($layerImg, $w, $h);
            $Lcolors = imagecolorsforindex($layerImg, $Lrgb);

            $Mrgb = imagecolorat($maskImg, $w, $h);
            $Mcolors = imagecolorsforindex($maskImg, $Mrgb);

            $alpha = $Mcolors["red"]/255*127;
            //$alpha = (int)( $alpha);

            imagesetpixel($imgtemp,$w,$h,imagecolorallocatealpha($imgtemp,$Lcolors["red"],$Lcolors["green"],$Lcolors["blue"],$alpha));
    }}

    imagepng($imgtemp, realpath(dirname(__FILE__))."\imgtem.png");

    //imagecopyresampled($imgtemp, $this->image, 0, 0, 0, 0, $this->width, $this->height, -$this->width, $this->height);     
    $this->image = $imgtemp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant