简体   繁体   English

PHP更改透明渐变png图像颜色

[英]PHP change transparent gradient png image color

I have image here (transparent PNG image) 我在这里有图片(透明PNG图片)

在此处输入图片说明

I want to change with blue one, is there any function(s) or library class to change my image? 我想用蓝色更改,是否有任何函数或库类更改我的图像? I know there are many website use their function to generate transparent gif with color. 我知道有很多网站使用它们的功能来生成带有颜色的透明gif。

Please help me. 请帮我。

$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);
}

This is untested code. 这是未经测试的代码。 The actual color transformation is left up to you, but as long as you use the same transformation across all indeces and don't muck with the alpha, the resulting image should retain the gradient. 实际的颜色转换由您自己决定,但是只要您在所有索引上使用相同的转换并且不浪费alpha值,生成的图像就应该保留渐变。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM