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

WBOY
Freigeben: 2016-06-23 13:44:21
Original
1404 Leute haben es durchsucht

贴上源码:

<?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 );}
Nach dem Login kopieren

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


回复讨论(解决方案)

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

未执行 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!
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!