这里有一个函数可以为图片着色渐变风格。
你只需要传递一个 img 对象和一个颜色数组。
例如:
$arr = array('#000000', '#990000', '#00FFFF', '#FFFFDD');
colorize ($img, $arr);
<?php
function colorize($imgdata, $palette)
{
imageTrueColorToPalette($imgdata,false,0xFF);
$l = count($palette)-1;
$i = imagecolorstotal($imgdata);
while ($i--)
{
list($r,$g,$b) = array_values(imageColorsForIndex($imgdata,$i));
$grayscale = ($r*.3 + $g*.59 +$b*.11) / 0xFF;
$pos = $l*$grayscale;
$perc = $pos-floor($pos);
$tbase = str_replace("#", '', $palette[$pos]);
$baseR = hexdec(substr($tbase,0,2));
$baseG = hexdec(substr($tbase,2,2));
$baseB = hexdec(substr($tbase,4,2));
$tmix = str_replace("#", '', $palette[$pos+1]);
$mixR = hexdec(substr($tmix,0,2));
$mixG = hexdec(substr($tmix,2,2));
$mixB = hexdec(substr($tmix,4,2));
$resR = $baseR+($mixR-$baseR)*$perc;
$resG = $baseG+($mixG-$baseG)*$perc;
$resB = $baseB+($mixB-$baseB)*$perc;
imagecolorset($imgdata, $i, $resR, $resG, $resB);
}
}
?>