Php - 替换透明png图像的底色

标签 php image colors png transparency

我搜索了很多,但只找到了很少的解决方案(在 google 和 stackoverflow 上,所以请不要将这个标记为重复,除非确实有重复的问题),但问题是硬边。是否有任何适当的方法来改变底色,比方说具有透明背景的黑色形状 png 图像,但要保留柔和的边缘?

这是一个示例图片:

enter image description here

我希望它看起来像这样:

enter image description here

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

enter image description here

由于我将在我的本地主机上使用它,仅供个人使用,因此感谢任何可以帮助实现此目的的 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);
}

最初,此功能是针对 GIF 图像(255 色调色板)的解决方案,所以我猜这就是出现硬边的原因。我正在寻找一种解决方案(对此脚本的改进)来保持 PNG 图像的透明度和软边缘。​​

编辑 2:

我在这里找到了一种使用 html5 canvas 和 javascript 的有趣方法: http://users7.jabry.com/overlord/mug.html

如果可能的话,也许有人会想到如何将其翻译成 PHP。

新解决方案

在回答中

最佳答案

这段代码没有举例说明问题,而是像这样转换颜色:

enter image description here

使用图像的 ALPHA channel 来确定着色。对于其他结果,只需使用 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">

关于Php - 替换透明png图像的底色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17733805/

相关文章:

php - 使用 JavaScript 和 PHP 的动态文本?

php - 需要时自动加载函数/类库

php - 如何加密 laravel 5.2 URL 或路由?

javascript - 在 JavaScript 中表达两个选项之间的随机选择的最优雅的方式

iOS 白色是一种颜色

java - 通过单击按钮更改 JFrame 颜色

php - 当数据库中的单词包含外语字母时找不到

android - 对于 android 开发,我可以在 ImageView 上使用 JPG 图像而不是 PNG 图像吗?

HTML 中的 javascript 如何使隐藏图像在函数调用中可见?

Android,如何实例化Universal Image Loader?