dongzhen4180 2009-07-23 20:19
浏览 56
已采纳

有没有人有脚本使用PHP创建水平渐变(从左到右)?

Here's the current code I am using.

<? header("Content-type: image/png");
// example: <img src="gradient.php?height=600&width=100&start=00FF00&end=ff0000" />
$height=100;
$width=1;
$start='000000';
$end='FFFFFF';
extract($_REQUEST); // overwrite using vars from url
$start_r = hexdec(substr($start,0,2));
$start_g = hexdec(substr($start,2,2));
$start_b = hexdec(substr($start,4,2));
$end_r = hexdec(substr($end,0,2));
$end_g = hexdec(substr($end,2,2));
$end_b = hexdec(substr($end,4,2));
$image = @imagecreate($width,$height);
for($y=0;$y<$height;$y++){
    for($x=0;$x<$width;$x++){
        if($start_r==$end_r) $new_r = $start_r;

        $difference = $start_r-$end_r;
        $new_r = $start_r-intval(($difference/$height)*$y); 

        if($start_g==$end_g) $new_g = $start_g;

        $difference = $start_g-$end_g;
        $new_g = $start_g-intval(($difference/$height)*$y);     

        if($start_b==$end_b) $new_b = $start_b;

        $difference = $start_b - $end_b;
        $new_b = $start_b-intval(($difference/$height)*$y);

        $row_color = imagecolorresolve($image,$new_r,$new_g,$new_b);
        imagesetpixel($image,$x,$y,$row_color);
    }    
}
imagepng($image);
imagedestroy($image);
?>

The above code works perfect in making vertical (top to bottom) gradients but I'd like to be able to make horizontal ones as well. I have a very good understanding for PHP, but I don't deal with PHP image functions very often. If someone can help me and figure this out I'd really appreciate it!

  • 写回答

3条回答 默认 最新

  • dousi2251 2009-07-24 05:21
    关注

    This code will work for vertical gradient and make it faster as well.

    I have commented out useless code so you know what to delete.

    for($x=0;$x<$width;$x++){
        /*if($start_r==$end_r) $new_r = $start_r;*/
        // ^^ the line above is useless, $new_r will be set below either way
    
        $difference = $start_r-$end_r;
        $new_r = $start_r-intval(($difference/$width)*$x); 
    
        /*if($start_g==$end_g) $new_g = $start_g;*/
        // ^^ the line above is useless, $new_g will be set below either way
    
        $difference = $start_g-$end_g;
        $new_g = $start_g-intval(($difference/$width)*$x);     
    
        /*if($start_b==$end_b) $new_b = $start_b;*/
        // ^^ the line above is useless, $new_b will be set below either way
    
        $difference = $start_b - $end_b;
        $new_b = $start_b-intval(($difference/$width)*$x);
    
        $new_color = imagecolorresolve($image,$new_r,$new_g,$new_b);
        // ^^ used to be $row_color
    
        for($y=0;$y<$height;$y++){
            imagesetpixel($image,$x,$y,$new_color);
        }    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制