想用一些渐变颜色的图片做背景,但无奈美工是一点都不会。所以只好用php来完成这个工作。
下面的例子是从蓝色渐变成红色。效果还算平滑。
PHP代码
- <?php
- $width = 500;
- $height = 20;
- $color1 = "0000FF";//blue
- $color2 = "FF0000";//red
- $im = imagecreatetruecolor($width, $height);
- list($R1, $G1, $B1) = RGBtodec($color1);
- list($R2, $G2, $B2) = RGBtodec($color2);
- for ($i=1; $i<=$width; $i++){
- $R = ($R2*$i+($width-$i)*$R1)/$width;
- $G = ($G2*$i+($width-$i)*$G1)/$width;
- $B = ($B2*$i+($width-$i)*$B1)/$width;
- $x1 = $i-1;
- $y1 = 0;
- $x2 = $i;
- $y2 = $height;
- $color = imagecolorallocatealpha($im, $R, $G, $B, 0);
- imagefilledrectangle ($im, $x1, $y1, $x2, $y2, $color);
- }
- imagepng($im);
- imagedestroy($im);
- function RGBtodec($rgb){
- $arr = array(0, 0, 0);
- if(strlen($rgb) == 6){
- $arr = array(
- substr($rgb, 0, 2), substr($rgb, 2, 2), substr($rgb, 4, 2)
- );
- }
- elseif (strlen($rgb) == 3){
- $arr = array(
- str_repeat(substr($rgb, 0, 1), 2),
- str_repeat(substr($rgb, 1, 1), 2),
- str_repeat(substr($rgb, 2, 1), 2)
- );
- }
- foreach ($arr as $k => $vl){
- $arr[$k] = hexdec($vl);
- }
- return $arr;
- }
- ?>