Link to home
Start Free TrialLog in
Avatar of stargateatlantis
stargateatlantis

asked on

Transparent mask

I got the following code but it creates a white mask.  How can i change it so that the mask is transparent?  
<?php
// 1. Start with the original image  
    $img = imagecreatefromjpeg("demo_files/crop.jpg");  
    $img_magicwhite = imagecolorallocatealpha($img, 255, 0, 255, 127);  
    //imagecolortransparent($img, $img_magicwhite);  

    // (Get its dimensions for copying)  
    list($w, $h) = getimagesize("demo_files/crop.jpg");  

    // 2. Create the first copy  
    $copy = imagecreatefromjpeg("demo_files/crop.jpg");  
    imagealphablending($copy, true);  

    $copy_magicwhite = imagecolorallocate($copy, 255, 0, 255);  
    imagecolortransparent($copy, $copy_magicwhite);  

    // 3. Create the mask  
    $mask = imagecreatetruecolor($w, $h);  
    imagealphablending($mask, true);  

    // 3-1. Set the masking colours  
    $mask_black = imagecolorallocate($mask, 0, 0, 0);  
    $mask_white = imagecolorallocate($mask, 255, 255, 255);  
    imagecolortransparent($mask, $mask_black);  
    imagefill($mask, 0, 0, $mask_white);  

    // 3-2. Draw the circle for the mask  
    $circle_x = $w/2;  
    $circle_y = $h/2;  
    $circle_w = $w-10;  
    $circle_h = $h;  
    imagefilledellipse($mask, $circle_x, $circle_y, $circle_w, $circle_h, $mask_black);
	
	

	 

    // 4. Copy the mask over the top of the copied image, and apply the mask as an alpha layer  
    imagecopymerge($copy, $mask, 0, 0, 0, 0, $w, $h, 100);  


    // 5. Do what you need to do to the image area  
    // My example is turning the original image gray and leaving the masked area as colour  
	    $x = imagesx($img);  
    $y = imagesy($img);  
    $gray = imagecreatetruecolor($x, $y);  
    imagecolorallocate($gray, 0, 0, 0);  
    for ($i = 0; $i > 16 & 0xFF;) 
	{  
        $g = ($rgb >> 8) & 0xFF;  
        $b = $rgb & 0xFF;  
         //for gray mode $r = $g = $b  
        $color = max(array($r, $g, $b));  
        $gray_color = imagecolorexact($img, $color, $color,   $color);  
        imagesetpixel($gray, $i, $j, $gray_color);  
      }  
  

    // 6. Merge the copy with the origianl - maintaining alpha  
    imagecopymergegray($gray, $copy, 0, 0, 0, 0, $w, $h, 100);  
    imagealphablending($gray, true);  
    imagecolortransparent($gray, $mask_white);  

    header('Content-Type: image/png');  
    imagepng($gray);  
    imagedestroy($gray); 
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial