图片校验的问题 现在要做个图片校验 但是以前没有做过 请各位能给贴个码 我可以参考下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //验证码一session_start(); session_register('SafeCode'); $type = 'gif'; $width= 50; //40$height= 20; //16header("Content-type: image/".$type); srand((double)microtime()*1000000); $randval = randStr(4,"NUMBER"); if($type!='gif' && function_exists('imagecreatetruecolor')){ $im = @imagecreatetruecolor($width,$height); }else{ $im = @imagecreate($width,$height); } $r = Array(225,211,255,223); $g = Array(225,236,237,215); $b = Array(225,236,166,125); $key = rand(0,3); $backColor = ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机) $borderColor = ImageColorAllocate($im, 0, 0, 0);//边框色 $pointColor = ImageColorAllocate($im, 255, 170, 255);//点颜色 @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置 @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置 $stringColor = ImageColorAllocate($im, 255,51,153); for($i=0;$i<=100;$i++){ $pointX = rand(2,$width-2); $pointY = rand(2,$height-2); @imagesetpixel($im, $pointX, $pointY, $pointColor); } @imagestring($im, 6, 5, 1, $randval, $stringColor); //3,5,1 大小,位置,位置$ImageFun='Image'.$type; $ImageFun($im); @ImageDestroy($im); $_SESSION['SafeCode'] = $randval; //产生随机字符串 function randStr($len=6,$format='ALL') { switch($format) { case 'ALL': //$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break; $chars='abcdefghijklmnopqrstuvwxyz0123456789'; break;case 'CHAR': //$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break; $chars='abcdefghijklmnopqrstuvwxyz'; break; case 'NUMBER': //$chars='0123456789'; break; $chars='0123456789'; break; default : //$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $chars='abcdefghijklmnopqrstuvwxyz0123456789'; break; } $string=""; while(strlen($string)<$len) $string.=substr($chars,(mt_rand()%strlen($chars)),1); return $string; } //*//*//验证码二session_start();//生成验证码图片 Header("Content-type: image/PNG"); srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用//session_start();//将随机数存入session中$_SESSION['authnum']="";$im = imagecreate(90,20) or die("Cant's initialize new GD image stream!"); //制定图片背景大小$red = ImageColorAllocate($im, 255,0,0); //设定三种颜色$white = ImageColorAllocate($im, 255,255,255); $gray = ImageColorAllocate($im, 200,200,200); //imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)imagefill($im,0,0,$white);//ed//生成数字和字母混合的验证码方法$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";$list=explode(",",$ychar);for($i=0;$i<4;$i++){ $randnum=rand(0,35); $authnum.=$list[$randnum]." ";//ed 加入一个空格}//while(($authnum=rand()%100000)<10000); //生成随机的四位数//将四位整数验证码绘入图片 $_SESSION['authnum']=$authnum;//int imagestring(resource image,int font,int x,int y,string s, int col)imagestring($im, 5, 10, 3, $authnum, $red);//用col颜色将字符串s画到image所代表的图像的x,y座标处(图像的左上角为0,0)。//如果 font 是 1,2,3,4 或 5,则使用内置字体for($i=0;$i<400;$i++){ //加入干扰象素 { $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));// imagesetpixel($im, rand()%90 , rand()%30 , $randcolor); imagesetpixel($im, rand()%90 , rand()%30 , $gray); } ImagePNG($im); ImageDestroy($im); //*//*验证码三session_start();session_register('code');$_SESSION['code'] = "";$width = "63";//图片宽60$height = "19";//图片高18$len = "4";//生成几位验证码$noise = true;//生成杂点$noisenum = 30;//杂点数量$image = imageCreate($width, $height);$back = imagecolorallocate($image,231,227,231); //背景色231,227,231imageFilledRectangle($image, 0, 0, $width, $height, $back);$size = $width/$len;if($size>$height) $size=$height;$left = ($width-$len*($size+$size/10))/$size;//生成随机字符串$textall=range('A','Z');for ($i=0; $i<$len; $i++) { $tmptext=rand(0, 25);$randtext = $textall[$tmptext]; $code .= $randtext;}//画干扰点和干扰线if($noise == true) setnoise();//画字符串for ($i=0;$i<strlen($code);$i++){ imageString($image,mt_rand(3,5),$i*$width/4+mt_rand(1,5),mt_rand(1,6),$code[$i],imageColorAllocate($image,mt_rand(50,255),mt_rand(0,120),mt_rand(50,255)));}$_SESSION['code'] = $code;header("Content-type: image/png");imagePng($image);imagedestroy($image);function setnoise(){global $image, $width, $height, $back, $noisenum;//画干扰点for ($i=0; $i<$noisenum; $i++){ $randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);} // 画出横向干扰线 for ($i = 1; $i <= 5; $i++) { ImageLine($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), 5); }}//*/ 上传多个图片并校验的代码 转自:http://www.phpzc.com/read.php?tid-754-fpage-2.html单张的图片上传是不复杂的,这里涉及到多张图片上传和对图片格式的校验,保证上传的一定是图片,防止上传其他文件到服务器。基本实现算法是使用数组的形式,把所有的图片提交个一个数组,对数组的元素进行一个个的处理。以下为引用的内容:<?php// 图片目录$img_dir = "../upload/";// …… html 显示上传界面 /* 图片上传处理 */// 把图片传到服务器// 初始化变量$uploaded = 0;$unuploaded = 0; //只允许五张图片上传for ($i=0; $i<=5; $i++){ //获取当前图片的信息$is_file = $_FILES['imgfile']['name'][$i];//如果当前图片不为空 if (!empty($is_file)) { //把当前图片的信息存储到变量里 $result[$i] = " <tr class=td2 align=center> <td>". $_FILES['imgfile']['name'][$i] ."</td> <td>". round($_FILES['imgfile']['size'][$i]/1024, 2) ."K</td> <td>". $_FILES['imgfile']['type'][$i] ."</td> <td>"; // 判断上传的图片的类型是不是jpg,gif,png,bmp中的一种,同时判断是否上传成功 if ( $_FILES['imgfile']['type'][$i] == "image/pjpeg" || $_FILES['imgfile']['type'][$i] == "image/gif" || $_FILES['imgfile']['type'][$i] == "image/x-png" || $_FILES['imgfile']['type'][$i] == "image/bmp" ) { //如果上传的文件没有在服务器上存在 if (!file_exists($img_dir . $_FILES['imgfile']['name'][$i])) { //把图片文件从临时文件夹中转移到我们指定上传的目录中 move_uploaded_file($_FILES['imgfile']['tmp_name'][$i], $img_dir . $_FILES['imgfile']['name'][$i]); $result[$i] .= "成功"; $uploaded++; } else //如果文件已经在服务器上存在 { $result[$i] .= "<font color=red>文件已存在</font>"; $unuploaded++; continue; } } else { $result[$i] .= "<font color=red>失败</font>"; $unuploaded++; } $result[$i] .= "</td></tr>"; } //end if} // end for // 如果没有选择任何图片if (empty($result)){ prompt_msg("错误信息", "没有选择任何图片。", "返回上一步", "uploadimg.php?action=upload" ); exit();}// 显示所有上传后的结果echo " <table cellpadding=4 cellspacing=1 border=0 class=table width=400 align=left> <tr class=navi align=center> <td>文件名</td> <td>大小</td> <td>类型</td> <td>上传结果</td> </tr> ";foreach( $result as $value){ echo $value;}echo "<tr class=td1> <td colspan=4>共上传 " . ($uploaded + $unuploaded) . ", 成功: $uploaded, 失败:<font color=red> $unuploaded</font> </td> </tr> <tr class=navi> <td colspan=4 align=center>[ <a href='uploadimg.php?action=upload' title='继续上传'>继续上传</a> ] [ <a href='image.php' title='浏览图片'>浏览图片</a> ]</td> </tr> </table> ";?> <?phpclass CaptchaSecurityImages { var $font = './cour.ttf';//这是图片显示的字体,这个你可以到C:\WINDOWS\Fonts找一个适合你自己的字体 function generateCode($characters) { $possible = '2345678bcdfhjkmnpqrstvwxyz'; $code = ''; $i = 0; while ($i < $characters) { $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); $i++; } return $code; } function CaptchaSecurityImages($width='146',$height='40',$characters='6') { $code = $this->generateCode($characters); $font_size = $height * 0.7; $image = @imagecreate($width, $height) or die('Cannot Initialize new GD image stream'); $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 62, 157, 174); $noise_color = imagecolorallocate($image, 99, 184, 199); for( $i=0; $i<($width*$height)/3; $i++ ) { imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); } for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); } $textbox = imagettfbbox($font_size, 0, $this->font, $code); $x = ($width - $textbox[4])/2; $y = ($height - $textbox[5])/2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code); imagejpeg($image); imagedestroy($image); unset($_SESSION['veryCode']); $_SESSION['veryCode'] = $code; }}$width = '146';$height = isset($_GET['height']) ? $_GET['height'] : '40';$characters = isset($_GET['characters']) ? $_GET['characters'] : '6';header('Content-Type: image/jpeg');$captcha = new CaptchaSecurityImages($width,$height,$characters);?> 如何判读PHP页面第一次加载 spry 结束语句. Smarty的问题,浏览器显示白板,但查看源文件正常, 100分 GBK的数据库怎么能存文件(不乱码) 高手帮忙看一下这个数据库错误是什么引起的? >>>数组重新排序的问题<<< PHP的电子购物网站,平均每天总页面点击5W次,有必要专用服务器吗? 如何获取下拉列表选定的值? 看不懂的正则表达式 十分简单的问题,ip怎么样比较呢? 这个网站怎么这么历害,测试了三天了,仍然没有进展,请大家帮忙 想做PHP开发,要些什么环境?在哪里可以下载呢??
//验证码一
session_start();
session_register('SafeCode');
$type = 'gif';
$width= 50; //40
$height= 20; //16
header("Content-type: image/".$type);
srand((double)microtime()*1000000);
$randval = randStr(4,"NUMBER");
if($type!='gif' && function_exists('imagecreatetruecolor')){
$im = @imagecreatetruecolor($width,$height);
}else{
$im = @imagecreate($width,$height);
}
$r = Array(225,211,255,223);
$g = Array(225,236,237,215);
$b = Array(225,236,166,125); $key = rand(0,3); $backColor = ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)
$borderColor = ImageColorAllocate($im, 0, 0, 0);//边框色
$pointColor = ImageColorAllocate($im, 255, 170, 255);//点颜色 @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置
@imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置
$stringColor = ImageColorAllocate($im, 255,51,153); for($i=0;$i<=100;$i++){
$pointX = rand(2,$width-2);
$pointY = rand(2,$height-2);
@imagesetpixel($im, $pointX, $pointY, $pointColor);
} @imagestring($im, 6, 5, 1, $randval, $stringColor); //3,5,1 大小,位置,位置
$ImageFun='Image'.$type;
$ImageFun($im);
@ImageDestroy($im);
$_SESSION['SafeCode'] = $randval;
//产生随机字符串
function randStr($len=6,$format='ALL') {
switch($format) {
case 'ALL':
//$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
$chars='abcdefghijklmnopqrstuvwxyz0123456789'; break;
case 'CHAR':
//$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
$chars='abcdefghijklmnopqrstuvwxyz'; break;
case 'NUMBER':
//$chars='0123456789'; break;
$chars='0123456789'; break;
default :
//$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$chars='abcdefghijklmnopqrstuvwxyz0123456789';
break;
}
$string="";
while(strlen($string)<$len)
$string.=substr($chars,(mt_rand()%strlen($chars)),1);
return $string;
}
//*//*//验证码二
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用
//session_start();//将随机数存入session中
$_SESSION['authnum']="";
$im = imagecreate(90,20) or die("Cant's initialize new GD image stream!"); //制定图片背景大小
$red = ImageColorAllocate($im, 255,0,0); //设定三种颜色
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
//imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)
imagefill($im,0,0,$white);//ed
//生成数字和字母混合的验证码方法
$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";
$list=explode(",",$ychar);
for($i=0;$i<4;$i++){
$randnum=rand(0,35);
$authnum.=$list[$randnum]." ";//ed 加入一个空格
}
//while(($authnum=rand()%100000)<10000); //生成随机的四位数
//将四位整数验证码绘入图片
$_SESSION['authnum']=$authnum;
//int imagestring(resource image,int font,int x,int y,string s, int col)
imagestring($im, 5, 10, 3, $authnum, $red);
//用col颜色将字符串s画到image所代表的图像的x,y座标处(图像的左上角为0,0)。
//如果 font 是 1,2,3,4 或 5,则使用内置字体for($i=0;$i<400;$i++){ //加入干扰象素 {
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
// imagesetpixel($im, rand()%90 , rand()%30 , $randcolor);
imagesetpixel($im, rand()%90 , rand()%30 , $gray);
}
ImagePNG($im);
ImageDestroy($im);
//*//*验证码三
session_start();
session_register('code');
$_SESSION['code'] = "";
$width = "63";//图片宽60
$height = "19";//图片高18
$len = "4";//生成几位验证码
$noise = true;//生成杂点
$noisenum = 30;//杂点数量
$image = imageCreate($width, $height);
$back = imagecolorallocate($image,231,227,231); //背景色231,227,231
imageFilledRectangle($image, 0, 0, $width, $height, $back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
//生成随机字符串
$textall=range('A','Z');
for ($i=0; $i<$len; $i++) {
$tmptext=rand(0, 25);
$randtext = $textall[$tmptext];
$code .= $randtext;
}
//画干扰点和干扰线
if($noise == true) setnoise();
//画字符串
for ($i=0;$i<strlen($code);$i++){
imageString($image,mt_rand(3,5),$i*$width/4+mt_rand(1,5),mt_rand(1,6),$code[$i],imageColorAllocate($image,mt_rand(50,255),mt_rand(0,120),mt_rand(50,255)));
}
$_SESSION['code'] = $code;
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);
function setnoise()
{
global $image, $width, $height, $back, $noisenum;
//画干扰点
for ($i=0; $i<$noisenum; $i++){
$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
}
// 画出横向干扰线
for ($i = 1; $i <= 5; $i++) {
ImageLine($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), 5);
}
}
//*/
转自:http://www.phpzc.com/read.php?tid-754-fpage-2.html单张的图片上传是不复杂的,这里涉及到多张图片上传和对图片格式的校验,保证上传的一定是图片,防止上传其他文件到服务器。
基本实现算法是使用数组的形式,把所有的图片提交个一个数组,对数组的元素进行一个个的处理。
以下为引用的内容:
<?php
// 图片目录
$img_dir = "../upload/";
// …… html 显示上传界面
/* 图片上传处理 */
// 把图片传到服务器
// 初始化变量
$uploaded = 0;
$unuploaded = 0;
//只允许五张图片上传
for ($i=0; $i<=5; $i++)
{
//获取当前图片的信息
$is_file = $_FILES['imgfile']['name'][$i];
//如果当前图片不为空
if (!empty($is_file))
{
//把当前图片的信息存储到变量里
$result[$i] = "
<tr class=td2 align=center>
<td>". $_FILES['imgfile']['name'][$i] ."</td>
<td>". round($_FILES['imgfile']['size'][$i]/1024, 2) ."K</td>
<td>". $_FILES['imgfile']['type'][$i] ."</td>
<td>";
// 判断上传的图片的类型是不是jpg,gif,png,bmp中的一种,同时判断是否上传成功
if (
$_FILES['imgfile']['type'][$i] == "image/pjpeg" ||
$_FILES['imgfile']['type'][$i] == "image/gif" ||
$_FILES['imgfile']['type'][$i] == "image/x-png" ||
$_FILES['imgfile']['type'][$i] == "image/bmp"
)
{
//如果上传的文件没有在服务器上存在
if (!file_exists($img_dir . $_FILES['imgfile']['name'][$i]))
{
//把图片文件从临时文件夹中转移到我们指定上传的目录中
move_uploaded_file($_FILES['imgfile']['tmp_name'][$i],
$img_dir . $_FILES['imgfile']['name'][$i]);
$result[$i] .= "成功";
$uploaded++;
}
else //如果文件已经在服务器上存在
{
$result[$i] .= "<font color=red>文件已存在</font>";
$unuploaded++;
continue;
}
}
else
{
$result[$i] .= "<font color=red>失败</font>";
$unuploaded++;
}
$result[$i] .= "</td></tr>";
} //end if
} // end for
// 如果没有选择任何图片
if (empty($result))
{
prompt_msg("错误信息", "没有选择任何图片。", "返回上一步", "uploadimg.php?action=upload" );
exit();
}
// 显示所有上传后的结果
echo " <table cellpadding=4 cellspacing=1 border=0 class=table width=400 align=left>
<tr class=navi align=center>
<td>文件名</td>
<td>大小</td>
<td>类型</td>
<td>上传结果</td>
</tr>
";
foreach( $result as $value)
{
echo $value;
}
echo "<tr class=td1>
<td colspan=4>共上传 " . ($uploaded + $unuploaded) . ", 成功: $uploaded, 失败:<font color=red> $unuploaded</font> </td>
</tr>
<tr class=navi>
<td colspan=4 align=center>[ <a href='uploadimg.php?action=upload' title='继续上传'>继续上传</a> ] [ <a href='image.php' title='浏览图片'>浏览图片</a> ]</td>
</tr>
</table>
";
?>
class CaptchaSecurityImages { var $font = './cour.ttf';//这是图片显示的字体,这个你可以到C:\WINDOWS\Fonts找一个适合你自己的字体 function generateCode($characters) { $possible = '2345678bcdfhjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
} function CaptchaSecurityImages($width='146',$height='40',$characters='6') {
$code = $this->generateCode($characters); $font_size = $height * 0.7;
$image = @imagecreate($width, $height) or die('Cannot Initialize new GD image stream'); $background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 62, 157, 174);
$noise_color = imagecolorallocate($image, 99, 184, 199); for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
} for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
} $textbox = imagettfbbox($font_size, 0, $this->font, $code);
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code); imagejpeg($image);
imagedestroy($image);
unset($_SESSION['veryCode']);
$_SESSION['veryCode'] = $code; }}
$width = '146';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) ? $_GET['characters'] : '6';header('Content-Type: image/jpeg');
$captcha = new CaptchaSecurityImages($width,$height,$characters);
?>