【发布时间】:2011-09-12 08:27:00
【问题描述】:
我这里有图片(透明PNG图片)
我想换成蓝色的,有什么函数或库类可以改变我的图像吗?我知道有很多网站使用它们的功能来生成带颜色的透明 gif。
请帮帮我。
【问题讨论】:
-
我刚刚将问题标题编辑为“PHP 更改透明渐变 png 图像颜色”谢谢。
标签: php image-processing php-gd
我这里有图片(透明PNG图片)
我想换成蓝色的,有什么函数或库类可以改变我的图像吗?我知道有很多网站使用它们的功能来生成带颜色的透明 gif。
请帮帮我。
【问题讨论】:
标签: php image-processing php-gd
$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,生成的图像应该保留渐变。
【讨论】: