PHP | imagecolorclosestalpha()函数

这个 imagecolorclosestalpha() 函数是PHP中的一个内置函数,用于获取给定图像中具有alpha值的最接近颜色的索引。此函数返回图像调色板中最接近指定RGB值和alpha级别的颜色索引。alpha值表示图像的透明度。

null

语法:

int imagecolorclosestalpha ( $image, $red, $green, $blue, $alpha )

参数: 此函数接受上述五个参数,如下所述:

  • $image: 它由一个图像创建函数返回,例如imagecreatetruecolor()。它用于创建图像的大小。
  • $red: 此参数用于设置红色分量的值。
  • $green: 此参数用于设置绿色分量的值。
  • $blue: 此参数用于设置蓝色组件的值。
  • $alpha: 此参数用于设置图像的透明度。$alpha的值介于0到127之间,其中0表示完全不透明,而127表示完全透明。

返回值: 此函数用于返回调色板中最近颜色的索引。

下面的程序说明了 imagecolorclosestalpha() PHP中的函数:

项目1:

PHP

<?php
// Convert an image into a palette-based image
$image = imagecreatefrompng(
imagetruecolortopalette( $image , false, 255);
// Find closest color in image
$output = imagecolorclosestalpha( $image , 155, 40, 200, 50);
$output = imagecolorsforindex( $image , $output );
$output = "({ $output [ 'red' ]}, { $output [ 'green' ]},
{ $output [ 'blue' ]}, { $output [ 'alpha' ]})";
echo "Closest match: " . $output . "" ;
imagedestroy( $image );
?>


输出:

Closest match: (100, 58, 108, 0)

项目2:

PHP

<?php
// Convert an image into a palette-based image
$image = imagecreatefrompng(
imagetruecolortopalette( $image , false, 255);
// Search the given rgb color.
$color = array (
array (155, 40, 200, 50),
array (235, 205, 188, 127),
array (135, 00, 132, 0),
);
// Loop to return the closest color match.
foreach ( $color as $id => $rgb )
{
$output = imagecolorclosestalpha( $image , $rgb [0],
$rgb [1], $rgb [2], $rgb [3]);
$output = imagecolorsforindex( $image , $output );
$output = "({ $output [ 'red' ]}, { $output [ 'green' ]},
{ $output [ 'blue' ]}, { $output [ 'alpha' ]})";
echo "Given color: ( $rgb [0], $rgb [1], $rgb [2], $rgb [3])
=> Closest match: $output <br>";
}
imagedestroy( $image );
?>


输出:

Given color: (155, 40, 200, 50) => Closest match: (100, 58, 108, 0) Given color: (235, 205, 188, 127) => Closest match: (100, 58, 108, 0) Given color: (135, 0, 132, 0) => Closest match: (100, 58, 108, 0) 

相关文章:

参考: http://php.net/manual/en/function.imagecolorclosestalpha.php

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享