php 图片 中文验证码

 更新时间:2016年11月25日 16:59  点击:1659

php 图片 中文验证码

<img src="verify_image.php" alt="点此刷新验证码" name="verify_code" width="65" height="20" border="0" id="verify_code" onclick="document.getElementById('verify_code').src='verify_image.php?'+Math.random();" style="cursor:pointer;" />

<?php
session_start();

$vi = new vCodeImage;
$vi->SetImage(1,4,65,20,80,1);

class vCodeImage{
 var $mode;  //1:数字模式,2:字母模式,3:数字字母模式,其他:数字字母优化模式
 var $v_num;  //验证码个数
 var $img_w;  //验证码图像宽度
 var $img_h;  //验证码图像高度
 var $int_pixel_num;  //干扰像素个数
 var $int_line_num;  //干扰线条数
 var $font_dir;   //字体文件相对路径
 var $border;   //图像边框
 var $borderColor;  //图像边框颜色

 function SetImage($made,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_dir='../font',$border=true,$borderColor='255,200,85'){
  if(!isset($_SESSION['vCode'])){
   session_register('vCode');
  }
  $_SESSION['vCode']="";
 
  $this->mode = $made;
  $this->v_num = $v_num;
  $this->img_w = $img_w;
  $this->img_h = $img_h;
  $this->int_pixel_num = $int_pixel_num;
  $this->int_line_num = $int_line_num;
  $this->font_dir = $font_dir;
  $this->border = $border;
  $this->borderColor = $borderColor;
  $this->GenerateImage();
 }

 function GetChar($mode){
  if($mode == "1"){
   $ychar = "0,1,2,3,4,5,6,7,8,9";
  }
  else if($mode == "2"){
   $ychar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  }
  else if($mode == "3"){
   $ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  }
  else
   $ychar = "3,4,5,6,7,8,9,A,B,C,D,H,K,P,R,S,T,W,X,Y";
  return $ychar;
 }
 
 function RandColor($rs,$re,$gs,$ge,$bs,$be){
  $r = mt_rand($rs,$re);
  $g = mt_rand($gs,$ge);
  $b = mt_rand($bs,$be);
  return array($r,$g,$b);
 }
 
 function GenerateImage(){
  $im = imagecreate($this->img_w,$this->img_h);

  $black = imagecolorallocate($im, 0,0,0);
  $white = imagecolorallocate($im, 255,255,255);
  $bgcolor = imagecolorallocate($im, 250,250,250);

  imagefill($im,0,0,$bgcolor);

  $fonts = ScanDir($this->font_dir);
  $fmax = count($fonts) - 2;

  $ychar = $this->GetChar($this->mode);
  $list = explode(",",$ychar);

  $x = mt_rand(2,$this->img_w/($this->v_num+2));
  $cmax = count($list) - 1;

  $v_code = '';

  for($i=0;$i<$this->v_num;$i++) //验证码
  {
   $randnum = mt_rand(0,$cmax);
   $this_char = $list[$randnum];
   $v_code .= $this_char;
   $size = mt_rand(intval($this->img_w/5),intval($this->img_w/4));
   $angle = mt_rand(-20,20);
   $y = mt_rand(($size+2),($this->img_h-2));
   if($this->border)
    $y = mt_rand(($size+3),($this->img_h-3));
   $rand_color = $this->RandColor(0,200,0,100,0,250);
   $randcolor = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
   $fontrand = mt_rand(2, $fmax);
   $font = "$this->font_dir/".$fonts[$fontrand];
   imagettftext($im, $size, $angle, $x, $y, $randcolor, $font, $this_char);
   $x = $x + intval($this->img_w/($this->v_num+1));
  }

  for($i=0;$i<$this->int_pixel_num;$i++){//干扰像素
   $rand_color = $this->RandColor(50,250,0,250,50,250);
   $rand_color_pixel = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
   imagesetpixel($im, mt_rand()%$this->img_w, mt_rand()%$this->img_h, $rand_color_pixel);
  }

  for($i=0;$i<$this->int_line_num;$i++){ //干扰线
   $rand_color = $this->RandColor(0,250,0,250,0,250);
   $rand_color_line = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
   imageline($im, mt_rand(0,intval($this->img_w/3)), mt_rand(0,$this->img_h), mt_rand(intval($this->img_w - ($this->img_w/3)),$this->img_w), mt_rand(0,$this->img_h), $rand_color_line);
  }

  if($this->border) //画出边框
  {
   if(preg_match("/^\d{1,3},\d{1,3},\d{1,3}$/",$this->borderColor)){
    $borderColor = explode(',',$this->borderColor);
   }
   $border_color_line = imagecolorallocate($im,$borderColor[0],$borderColor[1],$borderColor[2]);
   imageline($im, 0, 0, $this->img_w, 0, $border_color_line); //上横
   imageline($im, 0, 0, 0, $this->img_h, $border_color_line); //左竖
   imageline($im, 0, $this->img_h-1, $this->img_w, $this->img_h-1, $border_color_line); //下横
   imageline($im, $this->img_w-1, 0, $this->img_w-1, $this->img_h, $border_color_line); //右竖
  }

  imageantialias($im,true); //抗锯齿

  $time = time();
  $_SESSION['vCode'] = $v_code."|".$time; //把验证码和生成时间负值给$_SESSION[vCode]

  //生成图像给浏览器
  if (function_exists("imagegif")) {
      header ("Content-type: image/gif");
      imagegif($im);
  }
  elseif (function_exists("imagepng")) {
      header ("Content-type: image/png");
      imagepng($im);
  }
  elseif (function_exists("imagejpeg")) {
      header ("Content-type: image/jpeg");
      imagejpeg($im, "", 80);
  }
  elseif (function_exists("imagewbmp")) {
      header ("Content-type: image/vnd.wap.wbmp");
      imagewbmp($im);
  }
  else
      die("No Image Support On This Server !");
 
  imagedestroy($im);
 }
}
?>

php 生成验证码程序

<?php
$consts = 'bcdfghjkmnpqrstwxyz';
 $vowels = 'aei23456789';
 for ($x = 0; $x < 6; $x++)
 {
  $const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1);
  $vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1);
 }
 $radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
 $_SESSION['checkcode'] = $string = substr($radomstring,0,4); //only display 4 str
 / t up image, the first number is the width and the second is the height
 $imageX = strlen($radomstring)*5.5; //the image width
 $imageY = 20;      //the image height
 $im = imagecreatetruecolor($imageX,$imageY);

 //creates two variables to store color
 $background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250));
 $foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
         imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
         imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
         imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)));
 $foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80);  
 $middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160));
 $middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80);

 //fill image with bgcolor
 imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
 //writes string
 imagestring($im, 5, 3, floor(rand(0,5))-1, $string[0], $foregroundArr[rand(0,3)]);
 imagestring($im, 5, 13, floor(rand(0,5))-1, $string[1], $foregroundArr[rand(0,3)]);
 imagestring($im, 5, 23, floor(rand(0,5))-1, $string[2], $foregroundArr[rand(0,3)]);
 imagestring($im, 5, 33, floor(rand(0,5))-1, $string[3], $foregroundArr[rand(0,3)]);
 for($i=0; $i<strlen($string); $i++)
 {
  if(mt_rand(0,ord(substr($string,$i,1)))%2 == 0)
  {
   $string{$i} = ' ';
  }
 }
 imagestring($im, 2, 2, 0, $string, $foreground2);
 //strikethrough

 $border = imagecolorallocate($im, 133, 153, 193);
 //imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
 imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);

 $pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
 for ($i=0;$i<20;$i++)
 {
  imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
 }
 //rotate

 $middleground = imagecolorallocatealpha($im, rand(160, 200), rand(160, 200), rand(160, 200), 80);
 //random shapes
 for ($x=0; $x<15;$x++)
 {
  if(mt_rand(0,$x)%2==0)
  {
   imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
   imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
  }
  else
  {
   imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
   imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
  }
 }
 //output to browser
    header("content-type:image/png\r\n");
 imagepng($im);
 imagedestroy($im);
?>

php截屏代码

<?php
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://www.111cn.net");

/* Still working? */
while ($browser->Busy) {
    com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
?>


<?
/**
* 生成缩略图
* $srcName----为原图片路径
* $newWidth,$newHeight----分别缩略图的最大宽,高
* $newName----为缩略图文件名(含路径),默认为加入thumbnail
* @param string $srcName
* @param int $newWidth
* @param int $newHeight
* @param string $newName
* return viod
*/
function resizeImg($srcName,$newWidth,$newHeight,$newName=""
)
{
        if(
$newName==""
)
        {
                
$nameArr=explode('.',$srcName
);
                
$expName=array_pop($nameArr
);
                
$expName='thumbnail.'.$expName
;
                
array_push($nameArr,$expName
);
                
$newName implode('.',$nameArr
);
        }
        
$info ""
;
        
$data getimagesize($srcName,$info
);
        switch (
$data[2
])
        {
                case 
1
:
                        if(!
function_exists("imagecreatefromgif"
)){
                                echo 
"你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回"
;
                                exit();
                        }
                        
$im ImageCreateFromGIF($srcName
);
                        break;
                case 
2
:
                        if(!
function_exists("imagecreatefromjpeg"
)){
                                echo 
"你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回"
;
                                exit();
                        }
                        
$im ImageCreateFromJpeg($srcName
);
                        break;
                case 
3
:
                        
$im ImageCreateFromPNG($srcName
);
                        break;
        }
        
$srcW=ImageSX($im
);
        
$srcH=ImageSY($im
);
        
$newWidthH=$newWidth/$newHeight
;
        
$srcWH=$srcW/$srcH
;
        if(
$newWidthH<=$srcWH
){
                
$ftoW=$newWidth
;
                
$ftoH=$ftoW*($srcH/$srcW
);
        }
        else{
                
$ftoH=$newHeight
;
                
$ftoW=$ftoH*($srcW/$srcH
);
        }
        if(
$srcW>$newWidth||$srcH>$newHeight
)
        {
                if(
function_exists("imagecreatetruecolor"
))
                {
                        @
$ni ImageCreateTrueColor($ftoW,$ftoH
);
                        if(
$niImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH
);
                        else
                        {
                                
$ni=ImageCreate($ftoW,$ftoH
);
                                
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH
);
                        }
                }
                else
                {
                        
$ni=ImageCreate($ftoW,$ftoH
);
                        
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH
);
                }
                if(
function_exists('imagejpeg')) ImageJpeg($ni,$newName
);
                else 
ImagePNG($ni,$newName
);
                
ImageDestroy($ni
);
        }
        
ImageDestroy($im
);
}

resizeImg('123.JPG',150,150
);
?>


php+mysql 图象输入输出


我看了网上的例子~用了~发现图片进数据库没问题~但是关于输出部分总是报错~
代码如下
建库:
CREATE TABLE Images ( PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY, Image BLOB );
进库:<?php
     $Picture=$_POST[file];   //flie是 我那个图片上传页的表单的传递
         If($Picture != "none")
         { $PSize = filesize($Picture);
         $mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
         mysql_connect("localhost","root","") or die("Unable to connect to SQL server");
         @mysql_select_db("mymg") or die("Unable to select database");
         mysql_query("INSERT INTO images (image) VALUES ('$mysqlPicture')") or die("Cant Perform Query"); }
         else
         { echo"You did not upload any picture"; }
         ?>
程序到这里都没问题~我用CMD看了下 数据库里也有东西.
输出:
<html>
<body>
<?php
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
$result=mysql_query("SELECT * FROM Images") or die("Cant Perform Query");
While($row=mysql_fetch_object($result))
                { echo "<IMG SRC='Second.php ? PicNum=$row- > PicNum'/>"};!                 ?>
                 </body>
                 </html>
'Second.php:
          <?php
$result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum") or die("Cant perform Query");
$row=mysql_fetch_object($result);
Header( "Content-type: image/gif");
echo $row->image;
?>
[!--infotagslink--]

相关文章

  • js URLdecode()与urlencode方法支持中文解码

    下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。 代码如下 复制代码 ...2016-09-20
  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • 关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)

    最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24
  • jQuery Real Person验证码插件防止表单自动提交

    本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08
  • C#读取中文文件出现乱码的解决方法

    这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • JS实现随机生成验证码

    这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • Mysql在debian系统中不能插入中文的终极解决方案

    在debian环境下,彻底解决mysql无法插入和显示中文的问题Linux下Mysql插入中文显示乱码解决方案mysql -uroot -p 回车输入密码进入mysql查看状态如下:默认的是客户端和服务器都用了latin1,所以会乱码。解决方案:mysql>use...2013-10-04
  • linux mint 下mysql中文支持问题

    一.mysql默认不支持中文,它的server和db默认是latin1编码.所以我们要将其改变为utf-8编码,因为utf-8包含了地球上大部分语言的二进制编码 1.关闭mysql服务 sudo /etc/init.d/mysql stop 2.修改mysql配置文件 mysql配...2015-10-21
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04