在 php 上即时创建验证码图像
create on fly captcha image on php
我有一个动态创建验证码图像的代码,它显示图像损坏...这是代码
$code = $this->generateCode($characters);
/* font size will be 95% of the image height */
$font_size = $height * 1;
$image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image,0, 0, 0);
// $text_color_2 = imagecolorallocate($image, 100, 40, 20);
$noise_color = imagecolorallocate($image, 50, 40, 35);
$line_color = imagecolorallocate($image, 150, 140, 135);
// $noise_color_2 = imagecolorallocate($image, 150, 120, 100);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/15; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
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), $line_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
谁能告诉我我的代码有什么问题吗?
问题出在 BOM 字符上。我删除了它,现在它可以正常工作了。
我有一个动态创建验证码图像的代码,它显示图像损坏...这是代码
$code = $this->generateCode($characters);
/* font size will be 95% of the image height */
$font_size = $height * 1;
$image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image,0, 0, 0);
// $text_color_2 = imagecolorallocate($image, 100, 40, 20);
$noise_color = imagecolorallocate($image, 50, 40, 35);
$line_color = imagecolorallocate($image, 150, 140, 135);
// $noise_color_2 = imagecolorallocate($image, 150, 120, 100);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/15; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
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), $line_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
谁能告诉我我的代码有什么问题吗?
问题出在 BOM 字符上。我删除了它,现在它可以正常工作了。