+ -
当前位置:首页 → 问答吧 → 请教新浪验证码的背景是咋样做的?

请教新浪验证码的背景是咋样做的?

时间:2011-11-28

来源:互联网

http://m0.mail.sina.com.cn/classic/activedCode.php?1321770805793

新浪验证码的背景是咋样随机生成的?能否给个实例,谢谢

作者: xhfhyerdfbytrer   发布时间: 2011-11-28

咋样随机生成?就是随机生成呀 怎么答好呢?
就跟数字一样随机生成不行嘛

作者: PhpNewnew   发布时间: 2011-11-28

给个实例,让我自己看把

作者: xhfhyerdfbytrer   发布时间: 2011-11-28

PHP code


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



可以看看,改进改进。。

作者: ci1699   发布时间: 2011-11-28

绝招 一哭二闹三上吊 真快...
引用 3 楼 ci1699 的回复:

PHP code


<?php
header ("Content-type: image/png");
session_start();
$gg = strtoupper(substr(rand(),0,2));

//rand生成随机数,substr截取函数,strtoupper字符串转大写,imagecolorallocate生成颜色,imagesetp……

作者: PhpNewnew   发布时间: 2011-11-28

楼主别跑路了啊。记得给分呐。。

引用 4 楼 phpnewnew 的回复:

绝招 一哭二闹三上吊 真快...
引用 3 楼 ci1699 的回复:

PHP code


<?php
header ("Content-type: image/png");
session_start();
$gg = strtoupper(substr(rand(),0,2));

//rand生成随机数,substr截取函数,strtoupper字符串转大写,im……

作者: ci1699   发布时间: 2011-11-28

错了错了,是验证码的背景(很多多边形的)

作者: xhfhyerdfbytrer   发布时间: 2011-11-28

背景图片

作者: kyzy_yy_pm   发布时间: 2011-11-28

是动态生成的,绝对不是背景图

作者: xhfhyerdfbytrer   发布时间: 2011-11-28

PHP code

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





画方框例子

作者: ci1699   发布时间: 2011-11-28

引用 8 楼 xhfhyerdfbytrer 的回复:

是动态生成的,绝对不是背景图

你怎么知道是不是背景图呢?

作者: kyzy_yy_pm   发布时间: 2011-11-28

不是随机的额,新浪的背景还是很丰富的,随机性很强

作者: xhfhyerdfbytrer   发布时间: 2011-11-28

借助上面的那位的代码,你把椭圆换成矩阵(php里有这个方法),天上颜色就行了
PHP code
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);

作者: kyzy_yy_pm   发布时间: 2011-11-28

PHP code

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



这个你调调吧,就是这个了

作者: kyzy_yy_pm   发布时间: 2011-11-28

小兄弟你还在调啊。。楼主太懒了

引用 13 楼 kyzy_yy_pm 的回复:

PHP code

header ("Content-type: image/png");
session_start();
$gg = strtoupper(substr(rand(),0,2));

//rand生成随机数,substr截取函数,strtoupper字符串转大写,imagecolorallocate生成颜色,imagesetpixel画点函数……

作者: ci1699   发布时间: 2011-11-28

相关阅读 更多