2023-05-12 开启多语言插件支持……

使用PHP实现密保卡功能

php 苏 demo 2140℃ 0评论

密保卡入库

 

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

 

 

密保卡图片生成

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

 

 

密保卡验证

  1. public function test1($uid)
  2. {
  3.     $this->load->model(‘admin/m_users’);
  4.     $user = $this->m_users->sel($uid);
  5.     //post提交过来验证
  6.     if($this->is_post())
  7.     {
  8.         $codes = $this->m_mibao->get_codes_by_uid($uid);
  9.         $codes = unserialize($codes);
  10.         $is_true = true;
  11.         foreach($_SESSION[‘mibao’][‘keys’] as $key)
  12.         {
  13.             if($codes[$key] != $_POST[‘values’][$key])
  14.             {
  15.                 $is_true = false;
  16.             }
  17.         }
  18.         if($is_true)
  19.         {
  20.             die(‘正确’);
  21.         }
  22.         else
  23.         {
  24.             die(‘错误’);
  25.         }
  26.     }
  27.     else
  28.     {
  29.         //渲染视图
  30.         $info = $this->m_mibao->get_by_uid($uid);
  31.         $data = array();
  32.         $data[‘keys’] = $this->m_mibao->get_rand_keys($info[‘letter’]);
  33.         $_SESSION[‘mibao’][‘keys’] = $data[‘keys’];
  34.         $data[‘uid’] = $uid;
  35.         $this->load->view(‘test1’,$data);
  36.     }
打赏

转载请注明:苏demo的别样人生 » 使用PHP实现密保卡功能

   如果本篇文章对您有帮助,欢迎向博主进行赞助,赞助时请写上您的用户名。
支付宝直接捐助帐号oracle_lee@qq.com 感谢支持!
喜欢 (0)or分享 (0)