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

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.