当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


PHP imagesavealpha()用法及代码示例

imagesavealpha()函数是PHP中的内置函数,用于设置在保存PNG图像时是否保留完整的Alpha通道信息。 Alpha通道告诉图像是完全透明还是不透明。必须使用imagealphablending($im,false))禁用Alpha混合,以首先保留alpha-channel。

用法:

bool imagesavealpha( resource $image, bool $save_flag )

参数:该函数接受上述和以下描述的两个参数:


  • $image:它指定要处理的图像资源。
  • $save_flag:它指定是否保存Alpha通道。

返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。

以下示例说明了PHP中的imagesavealpha()函数:

范例1:在此示例中,我们将启用保存Alpha信息。

<?php 
  
// Load the png image 
$png = imagecreatefrompng( 
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png'); 
  
// Turn off alpha blending 
imagealphablending($png, false); 
  
// Add alpha as 100 to image 
$transparent = imagecolorallocatealpha($png, 255, 255, 255, 100); 
imagefill($png, 0, 0, $transparent); 
  
// Set alpha flag to true so that 
// alpha info is saved in the image 
imagesavealpha($png, true); 
  
// Save the image 
imagepng($png, 'imagewithalphainfo.png'); 
imagedestroy($png); 
?>

输出:

This will save the image in the same folder as imagewithalphainfo.png with alpha info.

范例2:在此示例中,我们将禁用保存Alpha信息。

<?php 
  
// Load the png image 
$png = imagecreatefrompng( 
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png'); 
  
// Turn off alpha blending 
imagealphablending($png, false); 
  
// Add alpha as 100 to image 
$transparent = imagecolorallocatealpha($png, 255, 255, 255, 100); 
imagefill($png, 0, 0, $transparent); 
  
// Set alpha flag to false so that 
// alpha info is not saved in the image 
imagesavealpha($png, false); 
  
// Save the image 
imagepng($png, 'imagewithoutalphainfo.png'); 
imagedestroy($png); 
?>

输出:

This will save the image in the same folder as 
imagewithoutalphainfo.png without alpha info.

参考: https://www.php.net/manual/en/function.imagesavealpha.php



相关用法


注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | imagesavealpha() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。