选择语言 :

 Core_Captcha::image_gradient

Fills the background with a gradient.

void Core_Captcha::image_gradient( resource $color1 , resource $color2 [, string $direction = null ] )

参数列表

参数 类型 描述 默认值
$color1 resource Gd image color identifier for start color
$color2 resource Gd image color identifier for end color
$direction string Direction: 'horizontal' or 'vertical', 'random' by default null
返回值
  • void
File: ./core/classes/captcha.class.php
protected function image_gradient($color1, $color2, $direction = null)
{
    $directions = array('horizontal', 'vertical');

    // Pick a random direction if needed
    if ( !in_array($direction, $directions) )
    {
        $direction = $directions[array_rand($directions)];

        // Switch colors
        if ( mt_rand(0, 1) === 1 )
        {
            $temp = $color1;
            $color1 = $color2;
            $color2 = $temp;
        }
    }

    // Extract RGB values
    $color1 = imagecolorsforindex(Captcha::$image, $color1);
    $color2 = imagecolorsforindex(Captcha::$image, $color2);

    // Preparations for the gradient loop
    $steps = ($direction === 'horizontal') ? Captcha::$config['width'] : Captcha::$config['height'];

    $r1 = ($color1['red'] - $color2['red']) / $steps;
    $g1 = ($color1['green'] - $color2['green']) / $steps;
    $b1 = ($color1['blue'] - $color2['blue']) / $steps;

    $i = null;
    if ( $direction === 'horizontal' )
    {
        $x1 = & $i;
        $y1 = 0;
        $x2 = & $i;
        $y2 = Captcha::$config['height'];
    }
    else
    {
        $x1 = 0;
        $y1 = & $i;
        $x2 = Captcha::$config['width'];
        $y2 = & $i;
    }

    // Execute the gradient loop
    for( $i = 0; $i <= $steps; $i ++ )
    {
        $r2 = $color1['red']   - floor($i * $r1);
        $g2 = $color1['green'] - floor($i * $g1);
        $b2 = $color1['blue']  - floor($i * $b1);
        $color = imagecolorallocate(Captcha::$image, $r2, $g2, $b2);

        imageline(Captcha::$image, $x1, $y1, $x2, $y2, $color);
    }
}