php做验证码输出图片显示的是乱码,怎么破?

php中文网
发布: 2016-06-23 13:44:21
原创
1565人浏览过

贴上源码:

<?phprequire_once 'string.func.php';function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $session_name = 'verify') {	session_start ();	// 创建画布	$width = 80;	$height = 28;	$image = imagecreatetruecolor ( $width, $height );	$white = imagecolorallocate ( $image, 255, 255, 255 );	$black = imagecolorallocate ( $image, 0, 0, 0 );	// 用填充矩形填充画布	imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white );	$chars = buildRandomString ( $type, $length );	$_SESSION [$session_name] = $chars;	$fontfiles = array (			"ARIAL.TTF",			"CALIBRI.TTF" 	);	for($i = 0; $i < $length; $i ++) {		$size = mt_rand ( 14, 18 );		$angle = mt_rand ( - 15, 15 );		$x = 5 + $i * $size;		$y = mt_rand ( 20, 26 );		$fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];		$color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );		$text = substr ( $chars, $i, 1 );		imagettftext ( $image, $size, $angle, $x, $y, $color, $fontfile, $text );	}	// 判断是否加干扰点	if ($pixel) {		for($i = 0; $i < $pixel; $i ++) {			imagesetpixel ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $black );		}	}	// 判断是否加干扰线	if ($line) {		for($i = 0; $i < $line; $i ++) {			$color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );			imageline ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $color );		}	}	header ( "Content-Type:image/gif" );	imagegif ( $image );	imagedestroy ( $image );}
登录后复制

求破!!!这个问题网上也查了很多,基本上有这三种错误:
1.有BOM头;我已经去掉了
2.GD库没有开;我已经开了
3.header前面不能有任何输出;我也没有任何输出
到底是什么原因?

析稿Ai写作
析稿Ai写作

科研人的高效工具:AI论文自动生成,十分钟万字,无限大纲规划写作思路。

析稿Ai写作 142
查看详情 析稿Ai写作

回复讨论(解决方案)

刚刚试了一下,我把获得图片的文件移动到项目的一个子文件夹中后,可以显示了。原来我是直接放在项目的根目录下的,获得的图片就是乱码,具体是什么原因我也不清楚。希望知道原因的能够分享出来!

未执行 verifyImage 函数,或拼写错误
string.func.php 加载失败
buildRandomString 函数名拼写错误
字体文件不存在

未执行 verifyImage 函数,或拼写错误
string.func.php 加载失败
buildRandomString 函数名拼写错误
字体文件不存在


我把调用verifyImage()函数的文件从根目录移到了子目录中就好了,这是什么原因?

$fontfile = " ../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];

位置变了,文件就找不到了
你不可能把字体文件放的到处都是吧?

$fontfile = " ../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];

位置变了,文件就找不到了
你不可能把字体文件放的到处都是吧?


没有 ,我的字体文件都放在fonts文件夹中, 路径不可能错的。

求大神来破诅咒!

已经好几天了 ,再没人来我就结贴了!


$fontfile = " ../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];

位置变了,文件就找不到了
你不可能把字体文件放的到处都是吧?


没有 ,我的字体文件都放在fonts文件夹中, 路径不可能错的。

那你知道   $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];
这其实是一个相对路径么?



$fontfile = " ../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];

位置变了,文件就找不到了
你不可能把字体文件放的到处都是吧?


没有 ,我的字体文件都放在fonts文件夹中, 路径不可能错的。

那你知道   $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];
这其实是一个相对路径么?
恩恩 ,知道了 ,原来放在根目录下,获得的路径不对 。
Thank You!
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号