Page 1 of 1

alpha or "punch out a color" help

Posted: Fri May 30, 2008 8:43 am
by kyledefranco
Hello all, I'm new here.
I'm developing a Flash app in which the user traces his or her face. Done. The coordinates are being sent to a PHP page which fills a polygon. Done. Now what I would like to do is instead of filling the polygon, convert it to alpha. Can this be done? I'm attaching my code and a screenshot of what I've done so far.
Any help is appreciated.

Code: Select all

<?
    /* INITIALIZE A VARIABLE */
        $coordinates=substr($_GET['c'],0,strlen($_GET['c'])-1);
    /* BUILD AN ARRAY */
        $temp=split(',',$coordinates);
    /* CREATE THE IMAGE */
        $image=imagecreatetruecolor(400, 370);
    /* USE A FILL */
        $fill_color=imagecolorallocate($image, 255, 255, 255);
    /* DRAW IT */
        imagefilledpolygon($image, $temp, (count($temp)/2), $fill_color);
    /* SET THE HEADER */
        header('Content-type: image/png');
    /* FLUSH THE IMAGE */
        imagepng($image);
        imagedestroy($image);
?>

Re: alpha or "punch out a color" help

Posted: Sat Jun 14, 2008 10:54 am
by onion2k
A nice prompt reply.. ;)

You need to set the image to retain the alpha channel, but also replace any alpha values rather than merging them. You also need to set the alpha value of the fill pixels to 127 (transparent)...

Code: Select all

<?php
 
    /* INITIALIZE A VARIABLE */
    $coordinates="20,60,
                  120,60,
                  160,120,
                  120,200";
    /* BUILD AN ARRAY */
        $temp=split(',',$coordinates);
    /* CREATE THE IMAGE */
        $image=imagecreatetruecolor(400, 370);
        imagesavealpha($image, true); //Save the alpha values
        imagealphablending($image, false); //Replace the alpha value for any filled pixel rather than adding them
    /* USE A FILL */
        $fill_color=imagecolorallocatealpha($image, 255, 255, 255, 127); //Allocate a color with an alpha channel value
    /* DRAW IT */
        imagefilledpolygon($image, $temp, (count($temp)/2), $fill_color);
    /* SET THE HEADER */
        header('Content-type: image/png');
    /* FLUSH THE IMAGE */
        imagepng($image);
        imagedestroy($image);