[ Team LiB ] Previous Section Next Section

Making a Color Transparent

PHP allows you to make selected colors within your image transparent with imagecolortransparent(), which requires an image resource and a color resource. When you output your image to the browser, the color you pass to imagecolortransparent() is transparent. Listing 15.7 changes our polygon code so that the shape floats on the browser instead of sitting against a background color.

Listing 15.7 Making Colors Transparent with imagecolortransparent()
 1: <?php
 2: header("Content-type: image/png");
 3:
 4: $image = imagecreate( 200, 200 );
 5: $red = imagecolorallocate($image, 255,0,0);
 6: $blue = imagecolorallocate($image, 0,0,255 );
 7:
 8: $points = array (   10, 10,
 9:       190, 190,
10:       190, 10,
11:       10, 190
12:       );
13:
14: imagefilledpolygon( $image, $points, count( $points )/2 , $blue );
15: imagecolortransparent( $image, $red );
16: imagepng($image);
17: ?>

Listing 15.7 is identical to Listing 15.6 except for the call to imagecolortransparent() on line 15. Figure 15.7 shows the output from Listing 15.7.

Figure 15.7. Making colors transparent with imagecolor transparent().

graphics/15fig07.gif

    [ Team LiB ] Previous Section Next Section