有没有人有脚本使用PHP创建水平渐变(从左到右)? [英] Does anyone have a script to create a horizontal gradient (left to right) using PHP?

查看:114
本文介绍了有没有人有脚本使用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);
?>

上面的代码非常适合制作垂直(从上到下)渐变,但我想成为能够制作水平的。我对PHP有很好的理解,但我不经常处理PHP图像功能。如果有人可以帮助我解决这个问题,我会非常感激!

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!

推荐答案

此代码适用于垂直渐变和制作它也更快。

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

这篇关于有没有人有脚本使用PHP创建水平渐变(从左到右)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆