Nik*_* R. 11 php png transparency image colors
我搜索了很多,我发现只有几个解决方案(谷歌和stackoverflow所以请不要将此标记为重复,除非有真正重复的问题),但问题是困难的边缘.有没有适当的方法来改变基色,让我们说黑色的形状png图像与透明背景,但保持柔软的边缘?
这是一个示例图像:

我希望它看起来像这样:

但我找到的解决方案给了我这个:

由于我将在我的本地主机上使用它,仅供个人使用,任何有助于实现此目的的PHP库都表示赞赏.
更新:
这是给我第三张图片的功能:
function LoadPNG($imgname)
{
$im = imagecreatefrompng ($imgname);
imagetruecolortopalette($im,false, 255);
$index = imagecolorclosest ( $im, 0,0,0 ); // GET BLACK COLOR
imagecolorset($im,$index,0,150,255); // SET COLOR TO BLUE
$name = basename($imgname);
imagepng($im, getcwd()."/tmp/$name" ); // save image as png
imagedestroy($im);
}
$dir = getcwd()."/img/";
$images = glob($dir."/*.png",GLOB_BRACE);
foreach($images as $image) {
LoadPNG($image);
}
Run Code Online (Sandbox Code Playgroud)
最初,这个功能是GIF图像的解决方案(255种颜色的调色板)所以我想这就是为什么有硬边缘.我正在寻找一个解决方案(改进这个脚本)来保持PNG图像的透明度和柔和边缘.
编辑2:
我在这里找到了一个使用html5 canvas和javascript的有趣方法:http: //users7.jabry.com/overlord/mug.html
也许有人可以知道如果可能的话如何将其翻译成PHP.
新解决方案
在答案中
Ste*_*eAp 14
此代码不能解释问题,但会转换如下颜色:

使用图像的ALPHA通道确定颜色.对于其他结果,只需玩imagecolorallocatealpha():
function colorizeBasedOnAplhaChannnel( $file, $targetR, $targetG, $targetB, $targetName ) {
$im_src = imagecreatefrompng( $file );
$width = imagesx($im_src);
$height = imagesy($im_src);
$im_dst = imagecreatefrompng( $file );
// Note this:
// Let's reduce the number of colors in the image to ONE
imagefilledrectangle( $im_dst, 0, 0, $width, $height, 0xFFFFFF );
for( $x=0; $x<$width; $x++ ) {
for( $y=0; $y<$height; $y++ ) {
$alpha = ( imagecolorat( $im_src, $x, $y ) >> 24 & 0xFF );
$col = imagecolorallocatealpha( $im_dst,
$targetR - (int) ( 1.0 / 255.0 * $alpha * (double) $targetR ),
$targetG - (int) ( 1.0 / 255.0 * $alpha * (double) $targetG ),
$targetB - (int) ( 1.0 / 255.0 * $alpha * (double) $targetB ),
$alpha
);
if ( false === $col ) {
die( 'sorry, out of colors...' );
}
imagesetpixel( $im_dst, $x, $y, $col );
}
}
imagepng( $im_dst, $targetName);
imagedestroy($im_dst);
}
unlink( dirname ( __FILE__ ) . '/newleaf.png' );
unlink( dirname ( __FILE__ ) . '/newleaf1.png' );
unlink( dirname ( __FILE__ ) . '/newleaf2.png' );
$img = dirname ( __FILE__ ) . '/leaf.png';
colorizeBasedOnAplhaChannnel( $img, 0, 0, 0xFF, 'newleaf1.png' );
colorizeBasedOnAplhaChannnel( $img, 0xFF, 0, 0xFF, 'newleaf2.png' );
?>
Original
<img src="leaf.png">
<br />
<img src="newleaf1.png">
<br />
<img src="newleaf2.png">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6197 次 |
| 最近记录: |