alpha or "punch out a color" help

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
kyledefranco
Forum Newbie
Posts: 1
Joined: Fri May 30, 2008 8:38 am

alpha or "punch out a color" help

Post 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);
?>
Attachments
screenshots.gif
screenshots.gif (56.23 KiB) Viewed 4530 times
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: alpha or "punch out a color" help

Post 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);
 
Post Reply