前几天接手新公司的业务,拿到代码放到本地的主机(apache)跑着没问题,放到线上(iis+php )丫竟然显示错误,好吧,为了显示专业,咱先百度。百度了段验证码图片生成的代码,新建文件名为:vf.php
<?php @header("Content-Type:image/png"); session_start(); $_SESSION['authnum'] = '';
$str = "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,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(",", $str); for($i=0; $i<4; $i++){ $randnum = rand(0, 60); $authnum .= $list[$randnum]; } $_SESSION['authnum'] = strtolower($authnum);
$im = @imagecreate(40, 20) or die("Cant's initialize new GD image stream!"); $text_color = imagecolorallocate($im, 255, 255, 255); //文本颜色 $background_color01 = imagecolorallocate($im, 255, 0, 0); //背景色1 $background_color02 = imagecolorallocatealpha($im, 255, 255, 255, 127); //背景色2 $noise_color = imagecolorallocate($im, 200, 200, 200); //干扰颜色 imagefill($im, 0, 0, $background_color02); //区域填充
imagestring($im, 5, 2, 2, $authnum, $text_color); /for($i=0; $i<400; $i++){ //加入干扰象素 imagesetpixel($im, rand()%90 , rand()%30 , $noise_color); }/ imagepng($im); imagedestroy($im); ?>
本地跑没问题,上传主机空间,丫显示了,完全没压力;//生成图像验证码 static public function buildImageVerify($width = 48, $height = 22, $randval = NULL, $verifyName = 'verify') { @header("Content-Type:image/png"); if (!isset($_SESSION)) { session_start(); //如果没有开启,session,则开启session } $randval = empty($randval) ? ("" . rand(1000, 9999)) : $randval; $_SESSION[$verifyName] = $randval; $length = 4; $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width; $im = imagecreate($width, $height); $r = array(225, 255, 255, 223); $g = array(225, 236, 237, 255); $b = array(225, 236, 166, 125); $key = mt_rand(0, 3);
$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机)
$borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
$pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //点颜色
@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
// 干扰
for ($i = 0; $i < 10; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for ($i = 0; $i < 25; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
}
for ($i = 0; $i < $length; $i++) {
imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
}
self::output($im, 'png');
}</pre>
output代码段 static protected function output($im, $type = 'png', $filename = '') { header("Content-type: image/" . $type); $ImageFun = 'image' . $type; if (empty($filename)) { $ImageFun($im); } else { $ImageFun($im, $filename); } imagepng($im); imagedestroy($im); exit; }远程代码测试,显示出问题。
if (!isset($_SESSION)) { session_start(); //如果没有开启,session,则开启session } @header("Content-Type:image/png"); $_SESSION[$verifyName] = ''; $str = "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,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(",", $str); for($i=0; $i<4; $i++){ $randnum = rand(0, 60); $authnum .= $list[$randnum]; } $_SESSION[$verifyName] = strtolower($authnum); $im = @imagecreate(40, 20) or die("Cant's initialize new GD image stream!"); $text_color = imagecolorallocate($im, 255, 255, 255); //文本颜色 $background_color01 = imagecolorallocate($im, 255, 0, 0); //背景色1 $background_color02 = imagecolorallocatealpha($im, 255, 255, 255, 127); //背景色2 $noise_color = imagecolorallocate($im, 200, 200, 200); //干扰颜色 imagefill($im, 0, 0, $background_color02); //区域填充 imagestring($im, 5, 2, 2, $authnum, $text_color);还是失败,只能继续百度了,不然要在服务器安装apache;
<?php session_start(); //启动Session会话 $image_width=70; //设置图像宽度 $image_height=18; //设置图像高度 ob_clean(); $new_number=$_GET[num]; //$new_number=5; $num_image=imagecreate($image_width,$image_height); //创建一个画布 imagecolorallocate($num_image,255,255,255); //设置画布的颜色 $black=imagecolorallocate($num_image,0,0,0); /**/for($i=0;$i<strlen($new_number);$i++){ //循环读取SESSION变量中的验证码 $font=mt_rand(3,5); //设置随机的字体 $x=mt_rand(1,8)+$image_width*$i/4; //设置随机字符所在位置的X坐标 $y=mt_rand(1,$image_height/4); //设置随机字符所在位置的Y坐标 $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //设置字符的颜色 imagestring($num_image,$font,$x,$y,$new_number[$i],$color); //水平输出字符 } ob_clean(); //清除输出 header("content-type:image/png"); //设置创建图像的格式 imagepng($num_image); //生成PNG格式的图像 imagedestroy($num_image); //释放图像资源 ?>
header("content-type:image/png");这个小小代码的身上,我放的位置在顶部一直报错,放到最近生成代码前,问题解决,一切OK。
不会看错误日志的phper们,要努力了..大师,你说的日志还有哪的?虚心求教~下面的是错误报错的截图<imgsrc="http://static.oschina.net/uploads/space/2013/1112/143630_aWG9_1017646.png"alt=""/>好吧,当我没说...
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。