說明
int imagecolortransparent ( resource $image [, int $color ] )
imagecolortransparent() 將 image 圖像中的透明色設定為 color。
image 是 imagecreatetruecolor() 返回的圖像標識符,color 是 imagecolorallocate() 返回的顏色標識符。
Note:
透明色是圖像的一種屬性,透明度不是顏色的屬性。一旦設定了某個顏色為透明色,
圖像中之前畫為該色的任何區域都成為透明的。
返回新透明色的標識符,如果省略 color 則返回當前透明色的標識符。
<?php
function setTransparency($new_image,$image_source)
{
$transparencyIndex = imagecolortransparent($image_source);
$transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
if ($transparencyIndex >= 0) {
$transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);
}
$transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
imagefill($new_image, 0, 0, $transparencyIndex);
imagecolortransparent($new_image, $transparencyIndex);
}
?>
Sample Usage: (resizing)
<?php
$image_source = imagecreatefrompng('test.png');
$new_image = imagecreatetruecolor($width, $height);
setTransparency($new_image,$image_source);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
?>