Transparent mask

I got the following code but it creates a white mask.  How can i change it so that the mask is transparent?  
// 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');  

Open in new window

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Slick812Connect With a Mentor Commented:
hello stargateatlantis, I tried to go through the code you have in your Code Snippet, and see what you are trying to do, you seem to have copyed that code form somewhere, as it does not seem to be your code, and I am not sure that the code works?
in the code it says -
// My example is turning the original image gray and leaving the masked area as colour
so I have my code block below as a color image that I add a Black and White border that has an Ellipse in center as color

please try my code and see if it does the -
//turning the original image gray and leaving the masked area as colour

ask questions if it does not do what you want
//image at least image 200 x 200 pixels
$img = imagecreatefromjpeg('images3/sungal3.jpg');
//$img is the first COLOR image, Use your own path for this 
$w = imagesx($img);
$h = imagesy($img);
$Grey3 = imagecreatetruecolor($w, $h);
imagealphablending($Grey3, false);
$bgcolor = imagecolorallocatealpha($Grey3, 230, 0, 0,127);
imagecopy($Grey3, $img, 0, 0, 0, 0, $w, $h);
imagefilter($Grey3, IMG_FILTER_GRAYSCALE);
// change the $Grey3 to greyscale with imagefilter
imageFilledEllipse($Grey3, $w/2,$h/2, $w*.9,$h*.9, $bgcolor);
// place a transparent Ellipse with imageFilledEllipse()
imagecopy($img, $Grey3, 0, 0, 0, 0, $w, $h);
//transparent copy greyscale with imagecopy
imagepng($img,'trns4.png');//save to disk, use your own path
//you can also save it as a jpg

<html><head><title>Trans PNG test</title></head><BODY BGCOLOR="#E3F7FF"><h2>GD Transparent PNG Image test</h2>
show result png inage
<p><img src="trns4.png"></p>

Open in new window

All Courses

From novice to tech pro — start learning today.