http://m0.mail.sina.com.cn/classic/activedCode.php?1321770805793新浪验证码的背景是咋样随机生成的?能否给个实例,谢谢

解决方案 »

  1.   

    <?php 
    header ("Content-type: image/png");           
    session_start();
    $gg = strtoupper(substr(rand(),0,2));     //rand生成随机数,substr截取函数,strtoupper字符串转大写,imagecolorallocate生成颜色,imagesetpixel画点函数,imagechar横向写字符$_SESSION['bb']=$gg[0]+$gg[1];$im = @imagecreate (59,25)
      or die ("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate ($im, 222, 222, 222);//设置干扰像素,防止被OCR
    for ($i=0;$i<=128;$i++)
    {
    $point_color = imagecolorallocate ($im, rand(0,255), rand(0,255), rand(0,255));
    imagesetpixel($im,rand(2,128),rand(2,38),$point_color);
    }//逐个画上验证码字符
     
    $text_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));
    $x = rand(5,10);
    $y = rand(5,10);
    imagechar ($im, 5, $x, $y, $gg[0], $text_color);
    $x = 20; 
    imagechar ($im, 5, $x, $y, "+", $text_color); 
    $x = 33;    
    $text_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));  
    imagechar ($im, 5, $x, $y, $gg[1], $text_color); 
    $x = 45; 
    imagechar ($im, 5, $x, $y, "=", $text_color); //输出PNG图像
    imagepng ($im);
    imagedestroy ($im);
    可以看看,改进改进。。
      

  2.   


    Header("Content-type: image/png");
       $im = ImageCreate (200, 100);
       $col_blk = ImageColorAllocate($im, 0,0,0);
       $col_orn = ImageColorAllocate($im, 255,192,0);
       $col_yel = ImageColorAllocate($im, 255,255,0);
       $col_red = ImageColorAllocate($im, 255,0,0);
       $col_grn = ImageColorAllocate($im, 0,255,0);
       $col_blu = ImageColorAllocate($im, 0,0,255);
      
       ImageFilledRectangle($im,20,10,100,50,$col_blu);
       ImageFilledRectangle($im,5,40,50,90,$col_red);
       ImageFilledRectangle($im,40,80,100,95,$col_orn);
       ImageFilledRectangle($im,90,35,110,90,$col_yel);
       // 以上是第一种着色。直接绘制矩形。
       // 我故意用四个不同颜色的矩形围起一小块区域,
       
       ImageFill($im,70,70,$col_grn);
       // 这是第二种着色。
      
       ImageRectangle($im,120,40,190,90,$col_grn);
       // 暂且画一个矩形来做框吧。事实上任何样子的边界都可以做框。
       ImageFilltoBorder($im,130,50,$col_grn,$col_orn);
       // 把绿色矩形框内涂成橙色。
       // 只要指定的点位于这个“框”的范围内即可,与该点在区域内的位置无关。
       // 这个函数其实是这样工作的:
       // 从指定的点开始,向外,寻找指定颜色的边界,如果找到,则停止,
       // 找不到,就把途经的点涂成需要的颜色。
      
       ImagePNG($im);
       ImageDestroy($im);
    画方框例子
      

  3.   

    借助上面的那位的代码,你把椭圆换成矩阵(php里有这个方法),天上颜色就行了
    header ("Content-type: image/png");           
    session_start();
    $gg = strtoupper(substr(rand(),0,2));     //rand生成随机数,substr截取函数,strtoupper字符串转大写,imagecolorallocate生成颜色,imagesetpixel画点函数,imagechar横向写字符$_SESSION['bb']=$gg[0]+$gg[1];$im = @imagecreate (500,80) or die ("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate ($im, 222, 222, 222);//设置干扰像素,防止被OCR
    //for ($i=0;$i<=128;$i++)
    for ($i=0;$i<=10;$i++)
    {
        $point_color = imagecolorallocate ($im, rand(0,255), rand(0,255), rand(0,255));
    $cx = rand(5, 450);
    $cy = rand(5, 75);
    $w = rand(5, 70);
    $h = rand(5, 20);
    $s = 1.25;
    $e = 1.25;
    $color = 120;
    imagearc (  $im ,  $cx ,  $cy ,  $w ,  $h ,  $s ,  $e ,  $color );//    imagesetpixel($im,rand(2,500),rand(2,80),$point_color);
    }//逐个画上验证码字符
     
    $text_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));//数字1
    $x = rand(5,100);
    $y = rand(5,40);
    imagechar ($im, 5, $x, $y, $gg[0], $text_color);//+
    $x = 150; 
    imagechar ($im, 5, $x, $y, "+", $text_color); //数字2
    $x = rand(260,380);    
    $text_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));  
    imagechar ($im, 5, $x, $y, $gg[1], $text_color); // =
    $x = 400; 
    imagechar ($im, 5, $x, $y, "=", $text_color); //输出PNG图像
    imagepng ($im);
    imagedestroy ($im);
      

  4.   


    header ("Content-type: image/png");           
    session_start();
    $gg = strtoupper(substr(rand(),0,2));     //rand生成随机数,substr截取函数,strtoupper字符串转大写,imagecolorallocate生成颜色,imagesetpixel画点函数,imagechar横向写字符$_SESSION['bb']=$gg[0]+$gg[1];$im = @imagecreate (500,80) or die ("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate ($im, 222, 222, 222);//设置干扰像素,防止被OCR
    //for ($i=0;$i<=128;$i++)
    for ($i=0;$i<=10;$i++)
    {
    $x1 = rand(-5, 490);
    $y1 = rand(-5, 75);
    $x2 = rand(300, 600);
    $y2 = rand(40, 90);
    $color = rand(10, 255);
    imagefilledrectangle($im, $x1, $y1, $x2, $y2, $color);// imagefilledarc($im, $cx, $cy, $w, $h, 10, $s, $e, $color);
    }//逐个画上验证码字符
     
    $text_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));//数字1
    $x = rand(5,100);
    $y = rand(5,40);
    imagechar ($im, 5, $x, $y, $gg[0], $text_color);//+
    $x = 150; 
    imagechar ($im, 5, $x, $y, "+", $text_color); //数字2
    $x = rand(260,380);    
    $text_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));  
    imagechar ($im, 5, $x, $y, $gg[1], $text_color); // =
    $x = 400; 
    imagechar ($im, 5, $x, $y, "=", $text_color); //输出PNG图像
    imagepng ($im);
    imagedestroy ($im);
    这个你调调吧,就是这个了