Recipe 17.7. Getting and Setting a Transparent Color


17.7.2. Problem

You want to set one color in an image as transparent. When the image is overlayed on a background, the background shows through the transparent section of the image.

17.7.3. Solution

Use ImageColorTransparent( ):

$color = ImageColorAllocate($image, $red, $green, $blue); ImageColorTransparent($image, $color);

17.7.4. Discussion

Both GIFs and PNGs support transparencies; JPEGs, however, do not. To refer to the transparent color within GD, use the constant IMG_COLOR_TRANSPARENT. For example, here's how to make a dashed line that alternates between black and transparent:

// make a two-pixel thick black and white dashed line $style = array($black, $black, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT); ImageSetStyle($image, $style);

To find the current transparency setting, take the return value of ImageColorTransparent( ) and pass it to ImageColorsForIndex( ):

$transparent = ImageColorsForIndex($image, ImageColorTransparent($image)); print_r($transparent); $transparent = ImageColorsForIndex($image, ImageColorTransparent($image)); print_r($transparent); Array (     [red] => 255     [green] => 255     [blue] => 255 )

The ImageColorsForIndex( ) function returns an array with the red, green, and blue values. In this case, the transparent color is white.

17.7.5. See Also

Documentation on ImageColorTransparent( ) at http://www.php.net/imagecolortransparent and on ImageColorsForIndex( ) at http://www.php.net/imagecolorsforindex.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net