使用PHP实现密保卡功能

  密保卡入库

  1. $this->load->model('admin/m_mibao'); 
  2.  
  3. $data = array();     
  4. //生成随机横坐标 
  5. $rand_str = $this->_rand_str(10); 
  6.  
  7. $arr   = array(); 
  8. for($k=0;$k<strlen($rand_str);$k++) 
  9.     for ($i = 1;$i < 9;$i++) 
  10.     {            
  11.         $rand = $this->_rand_num(3);  
  12.         //赋给所有code的容器 
  13.         $arr[$rand_str{$k} . $i] =  $rand
  14.     } 
  15.  
  16. $data['code']     = serialize($arr); //序列化后将信息入库 
  17. $data['letter']   = $rand_str
  18.  
  19. //避免重复序列号 
  20. while (TRUE) 
  21.         $data['card_num'] = $this->_rand_num(20); 
  22.         //判断序列号是否重复存在 
  23.     if($this->m_mibao->has_card_num($data['card_num']) == 0) 
  24.     { 
  25.         break
  26.     } 
  27.  
  28. $data['add_time'] = TIME; 
  29. $data['user_id']  = $uid
  30.            
  31. echo $this->mibao->insert($data); 

 

 

  密保卡图片生成

  1. //就是算坐标 
  2.        public function show($user_id
  3.     $this->load->model('admin/m_mibao'); 
  4.     $info = $this->m_mibao->get_by_uid($user_id); 
  5.     if(emptyempty($info)) 
  6.     { 
  7.         $this->msg('该用户无密保卡!','admin-index'); 
  8.     } 
  9.      
  10.     $codes = unserialize($info['code']); 
  11.  
  12.     //图片初始值 
  13.      
  14.     $bit = 3;  //密保卡位数 
  15.      
  16.     $height = 332;    //图片高度 
  17.      
  18.     $width =  626;    //图片宽度 
  19.      
  20.     $im = imagecreatetruecolor($width,$height); 
  21.      
  22.     $linecolor = imagecolorallocate($im, 229,229,229); 
  23.      
  24.     $fontcolor = imagecolorallocate($im, 0, 0, 0); 
  25.      
  26.     $top_rectangle_color = imagecolorallocate($im,241,254,237); 
  27.      
  28.     $top_letter_color = imagecolorallocate($im,54,126,76); 
  29.      
  30.     $left_rectangle_color = imagecolorallocate($im,243,247,255); 
  31.      
  32.     $left_num_color = imagecolorallocate($im,4,68,192); 
  33.      
  34.     $logo_str_color = imagecolorallocate($im,0,0,0); 
  35.      
  36.     imagefill($im,0,0,imagecolorallocate($im,255,255,255));  //图片背景色 
  37.      
  38.     $font    = './public/baomi/fonts/simsun.ttc';  //字体 
  39.      
  40.     $font_en = './public/baomi/fonts/CONSOLA.TTF'//英文字体 
  41.      
  42.     $font2   = './public/baomi/fonts/simhei.ttf'//密保卡上方黑体 
  43.      
  44.      
  45.     $dst = imagecreatefromjpeg("./public/baomi/120.jpg"); 
  46.      
  47.      
  48.      
  49.     imagecopymerge($im,$dst,120,15,0,0,193,55,100); 
  50.      
  51.     imageline($im,10,72,$width-10,72,$linecolor); 
  52.      
  53.     $ltext = "电子密保卡"
  54.      
  55.     if(!imagettftext($im,10,0,340,47,$logo_str_color,$font2,$ltext)) { 
  56.         exit('error');    
  57.     } 
  58.      
  59.     //写入卡号 
  60.      
  61.     $b = '1000' . $info['card_num']; 
  62.      
  63.     for($i=0;$i<7;$i++){ 
  64.      
  65.         $p.= substr($b,3*$i,4). ' '
  66.      
  67.     } 
  68.      
  69.     $x = 40; $y = 95; //序列号位置 
  70.      
  71.     imagettftext($im,10,0,$x,$y,$color,$font,'序列号'); 
  72.      
  73.     imagettftext($im,11,0,$x+50,$y,$color,$font_en,$p); 
  74.      
  75.     //颜色框 
  76.      
  77.     imagefilledrectangle($im,10,106,$width-10,128,$top_rectangle_color); 
  78.      
  79.     imagefilledrectangle($im,10,129,65,$height-10,$left_rectangle_color); 
  80.      
  81.     //写入最上排英文字母及竖线 
  82.      
  83.     for($i=1;$i<=10;$i++){ 
  84.      
  85.         $x = $i*55+35;      $y = 123;    $float_size = 11;   //字母位置参数                
  86.      
  87.         imagettftext($im,$float_size,0,$x,$y,$top_letter_color,$font_en,$info['letter']{$i-1});//写入最上排英文字母 
  88.      
  89.     } 
  90.      
  91.     for($i=0;$i<=9;$i++){ 
  92.      
  93.         $linex = $i*55+65;    $liney = 105;    $liney2 = $height-10;  //竖线位置参数 
  94.      
  95.         imageline($im,$linex,$liney,$linex,$liney2,$linecolor);//划入竖线 
  96.      
  97.     } 
  98.      
  99.          
  100.      
  101.     //写入竖排数字及填入矩阵数据 划横线 
  102.      
  103.     for($j=0;$j<8;$j++){ 
  104.      
  105.         $jj=$j+1; 
  106.      
  107.         $x=35;  $y=($jj*24)+123; //左排数字及横线位置参数 
  108.      
  109.         imagettftext($im$float_size, 0, $x$y$left_num_color$font_en$jj);//写入左排数字 
  110.      
  111.         for($i=1;$i<=10;$i++){ 
  112.      
  113.             $float_size2=11;  $x = $i*55+27;  $sy=$y;  //填入矩阵数据位置参数 
  114.      
  115.            $s = $info['letter']{$i-1}; 
  116.            $s .= $j + 1; 
  117.            imagettftext($im,$float_size2,0,$x,$sy,$fontcolor,$font_en,$codes[$s]);//写入矩阵数据 
  118.      
  119.         } 
  120.     } 
  121.      
  122.     for($j=0;$j<10;$j++){ 
  123.      
  124.         $line_x=10; $line_x2=$width-10;$y=$j*24+105; //横线位置参数 y坐标数据同上 
  125.      
  126.         imageline($im,$line_x,$y,$line_x2,$y,$linecolor);//划入横线 
  127.      
  128.     } 
  129.      
  130.     //外框边线 
  131.      
  132.     imageline($im,10,10,$width-10,10,$linecolor);//横线 
  133.      
  134.     //imageline($im,10,$height-10,$width-10,$height-10,$linecolor); 
  135.      
  136.     imageline($im,10,10,10,$height-10,$linecolor);//竖线 
  137.      
  138.     imageline($im,$width-10,10,$width-10,$height-10,$linecolor); 
  139.      
  140.     //生成图片 
  141.      
  142.     ob_clean(); 
  143.     header("Content-type: image/jpeg"); 
  144.     imagejpeg($im,null,100); 
  145.     imagedestroy($im);       

 

 

  密保卡验证

  1. public function test1($uid
  2.  
  3.     $this->load->model('admin/m_users');         
  4.     $user = $this->m_users->sel($uid); 
  5.  
  6.     //post提交过来验证 
  7.     if($this->is_post()) 
  8.     { 
  9.         $codes = $this->m_mibao->get_codes_by_uid($uid); 
  10.         $codes = unserialize($codes); 
  11.  
  12.         $is_true = true; 
  13.         foreach($_SESSION['mibao']['keys'as $key
  14.         { 
  15.             if($codes[$key] != $_POST['values'][$key]) 
  16.             { 
  17.                 $is_true = false; 
  18.             } 
  19.         } 
  20.          
  21.         if($is_true
  22.         {            
  23.             die('正确'); 
  24.         } 
  25.         else  
  26.         { 
  27.              
  28.             die('错误'); 
  29.         } 
  30.     } 
  31.     else 
  32.     { 
  33.         //渲染视图 
  34.         $info = $this->m_mibao->get_by_uid($uid); 
  35.         $data = array(); 
  36.         $data['keys'] = $this->m_mibao->get_rand_keys($info['letter']); 
  37.         $_SESSION['mibao']['keys'] = $data['keys']; 
  38.         $data['uid'] = $uid
  39.         $this->load->view('test1',$data); 
  40.     } 
posted @ 2011-10-25 14:46  新邦PHP  阅读(149)  评论(0编辑  收藏  举报