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

alpha or "punch out a color" help

Postby kyledefranco » Fri May 30, 2008 8:43 am

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.
Syntax: [ Download ] [ Hide ]
<?
    /* 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 921 times
kyledefranco
Forum Newbie
 
Posts: 1
Joined: Fri May 30, 2008 8:38 am

Re: alpha or "punch out a color" help

Postby onion2k » Sat Jun 14, 2008 10:54 am

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)...

Syntax: [ Download ] [ Hide ]
<?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);
 
User avatar
onion2k
Jedi Mod
 
Posts: 5263
Joined: Tue Dec 21, 2004 6:03 pm
Location: usrlab.com


Return to PHPGD.com

Who is online

Users browsing this forum: No registered users and 3 guests