当前位置:首页> PHP教程> php进阶
关键字:
文章内容
图形随机码的生成。。
 
 
修改时间:[2011/04/29 16:20]    阅读次数:[1764]    发表者:[起缘]
 
效果:


<?php

$im 
= imagecreate(48, 25);
    
    
$backgroundcolor = imagecolorallocate ($im, 255, 255, 255);
    
$black = imagecolorallocate($im,100,100,100);
    
$bordercolor = imagecolorallocate($im , 150, 150, 150);
    
    
//debug 增加图片的掺杂特性
    
$linenums = mt_rand(10, 32);
    for(
$i=0; $i <= $linenums; $i++) {
        
$linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
        
$linex = mt_rand(0, 48);
        
$liney = mt_rand(0, 25);
        
imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
    }    
    
    for(
$i=0; $i <= 64; $i++) {
        
$pointcolor = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 255), mt_rand(50, 255));
        
imagesetpixel($im, mt_rand(0, 48), mt_rand(0, 25), $pointcolor);
    }
    
    
$string = "";
      
$arr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','a','b');//把想要码的填进来
      
srand((double) microtime()*10000000);
      
$randcode = array_rand($arr,4);
      for(
$i=0;$i<4;$i++){//生成4位随机数
      
$string .= $arr[$randcode[$i]];
      }

    
    
imagerectangle($im, 0, 0, 47, 24, $bordercolor);
    
imagestring($im,7,4,4,$string,$black);    
    
//debug 合成图片
    
header('Content-type: image/png');
    
imagepng($im);
    
imagedestroy($im);

?> 


(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)