在php脚本上生成验证码图像时出错

在php脚本上生成验证码图像时出错,php,Php,我正在尝试生成一个算法来进行验证码验证,但脚本返回错误“图像”无法显示,因为它包含错误 这两个循环中的一个可能是错误的部分,但我对php没有太多经验,也不能接受它。这里有什么问题?谢谢 <?php session_start(); $width = 120; $height = 40; $length = 5; $baseList = '0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $code =

我正在尝试生成一个算法来进行验证码验证,但脚本返回错误“图像”无法显示,因为它包含错误

这两个循环中的一个可能是错误的部分,但我对php没有太多经验,也不能接受它。这里有什么问题?谢谢

<?php
session_start();

$width  = 120;
$height =  40;
$length =   5;

$baseList = '0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

$code    = "";
$counter = 0;

$image = @imagecreate($width, $height) or die('Cannot initialize GD!');

for( $i=0; $i<10; $i++ ) {
   imageline($image,mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height),imagecolorallocate($image, mt_rand(150,255),mt_rand(150,255), mt_rand(150,255)));
}

for( $i=0, $x=0; $i<$length; $i++ ) {
   $actChar = substr($baseList, rand(0, strlen($baseList)-1), 1);
   $x += 10 + mt_rand(0,10);
   imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar,imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155),mt_rand(0,155)));
   $code .= strtolower($actChar);
}
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['securityCode'] = $code;
?>


此代码在我的系统中运行良好我认为显示图像没有任何问题。最好再次运行代码并返回错误如果您是php新手,请使用控制台窗口调试脚本错误。

那么脚本返回什么错误?您的代码在我的笔记本电脑上运行良好。(PHP5.5.30)您知道如何将生成的图像导出为.jpg图像吗?谢谢