【问题标题】:PHP change transparent gradient png image colorPHP更改透明渐变png图像颜色
【发布时间】:2011-09-12 08:27:00
【问题描述】:

我这里有图片(透明PNG图片)

我想换成蓝色的,有什么函数或库类可以改变我的图像吗?我知道有很多网站使用它们的功能来生成带颜色的透明 gif。

请帮帮我。

【问题讨论】:

  • 我刚刚将问题标题编辑为“PHP 更改透明渐变 png 图像颜色”谢谢。

标签: php image-processing php-gd


【解决方案1】:
$img = imagecreatefromgif("put here your image path");

// Grab all color indeces for the given image.
$indeces = array();
for ($y = 0; $y < $imgHeight; ++$y) {
    for ($x = 0; $x < $imgWidth; ++$x) {
        $index = imagecolorat($img, $x, $y);
        if (!in_array($index, $indeces)) {
            $indeces[] = $index;
        }
    }
}   

foreach ($indeces as $index) {
    // Grab the color info for the index.
    $colors = imagecolorsforindex($img, $index);

    // Here, you would make your color transformation.
    $red    = $colors['red'];
    $green  = $colors['green'];
    $blue   = $colors['blue'];
    $alpha  = $colors['alpha'];

    // Update the old color to the new one.
    imagecolorset($img, $index, $red, $green, $blue, $alpha);
}

这是未经测试的代码。实际的颜色转换由您决定,但只要您在所有索引中使用相同的转换并且不使用 alpha,生成的图像应该保留渐变。

参考:http://www.php.net/manual/en/ref.image.php

【讨论】:

    猜你喜欢
    • 2014-02-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多