Simple Transparency

 

Improving the Clock
The clock would look a lot better if we got rid of the white box around it, leaving just the circle. This is accomplished by setting one of our indexed colors the special color "transparent". Any pixels in the image colored with this index will appear transparent in the final image, allowing the background to show through.  
int imageColorTransparent ( resource image [, int color])
Sets the transparent color in the image image to color. image is the image identifier returned by imageCreate() and color is a color identifier returned by imageColorAllocate(). The identifier of the new (or current, if none is specified) transparent color is returned.
<?PHP
/*
clock2.php
Generates an analog clock png from the current time
on a transparent background
*/

// Step 1. Create a new blank image
$im imageCreate(101101);

// Step 2. Add Content
// begin by defining the colors used in the image
// this time, the first color defined will become transparent
$background imageColorAllocate ($im000);
$background imageColorTransparent($im,$background);

$nyphpPurp imageColorAllocate ($im0x700x6D0x85); 
$nyphpBlue imageColorAllocate ($im0x1A0x1A0x73); 
$nyphpGrey imageColorAllocate ($im0xDD0xDD0xDD); 

// then use those colors to draw a series of overlapping circles
imageFilledEllipse ($im5050100100$nyphpBlue); 
imageFilledEllipse ($im5050,  90,  90$nyphpPurp); 
imageFilledEllipse ($im5050,  75,  75$nyphpGrey);

// for the hands, calculate the degrees for arc from current time
$hd imgDegreesFromTime('hour') ;
$md imgDegreesFromTime('minute') ;
$sd imgDegreesFromTime('second') ;

// draw the hands using degrees calculated above with small offsets
imageFilledArc ($im50505252$hd-6$hd+6$nyphpPurpIMG_ARC_PIE); 
imageFilledArc ($im50506565$md-3$md+2$nyphpPurpIMG_ARC_PIE); 
imageFilledArc ($im50507070$sd-2$sd+1$nyphpBlueIMG_ARC_PIE); 

// add a final small dot on top
imageFilledEllipse ($im5050,   5,   5$nyphpPurp); 

// Steps 3-5. Send headers, image data, & destroy image
header ('Refresh: 1; URL='.$_SERVER['PHP_SELF']); 
header('Content-type: image/png');
imagePNG ($im); 
imageDestroy ($im); 

/**
 * @param $kind string    'hour', 'minute' or 'second'
 * @return int            calculated degrees w/ 0 at 3 o'clock
 * @desc converts the hour, minute or second to 360¼ value
 *       shifted to 3 o'clock for use in GD image functions
 */
function imgDegreesFromTime($kind) {
    switch (
$kind) {
        case 
'hour' :
            return ((
date('g') * 30) + 270) % 360 // 30 = 360 / 12 hours
        
case 'minute' :
            return ((
date('i') * 6)  + 270) % 360 //  6 = 360 / 60 minutes
        
case 'second' :
            return ((
date('s') * 6)  + 270) % 360 //  6 = 360 / 60 seconds
    
}
}
?>