Php .ttf文件不工作
我有一个Php .ttf文件不工作,php,css,captcha,Php,Css,Captcha,我有一个.ttf文件要加载到验证码图像中。但是我的.ttf就是不起作用。当我包含.ttf文件时,图像不会加载并给出错误信息 这是我的验证码: <?php session_start(); $md5_hash = md5(rand(0,999)); $captcha = substr($md5_hash, 16, 5); $_SESSION["captcha"] = $captcha; $width = 205; $height = 60; $image = imagecreat
.ttf
文件要加载到验证码图像中。但是我的.ttf
就是不起作用。当我包含.ttf
文件时,图像不会加载并给出错误信息
这是我的验证码:
<?php
session_start();
$md5_hash = md5(rand(0,999));
$captcha = substr($md5_hash, 16, 5);
$_SESSION["captcha"] = $captcha;
$width = 205;
$height = 60;
$image = imagecreate($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$grey = imagecolorallocate($image, 204, 204, 204);
imagefill($image, 0, 0, $white);
$font = imageloadfont('Xeron.ttf');
imagestring($image, $font, 70, 20, $captcha, $black);
imagerectangle($image, 0, 0, $width-1, $height-1, $grey);
imageline($image, 0, $height/2, $width, $height/2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>
我提供了一个php验证码的示例代码。。。您可以尝试使用您的值、字体、字体颜色等 captcha.php
<?php
session_start();
//Settings: You can customize the captcha here
$image_width = 210;
$image_height = 60;
$characters_on_image = 7;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789ABDEFGHLMNQRTWY';
$random_dots = 15;
$random_lines = 3;
$captcha_text_color="#4593BC";
$captcha_noice_color = "#34495e";
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.6;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser window
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['captcha'] = $code;
function hexrgb ($hexstr){
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
?>
//to display captcha image
<img src="captcha.php?rand=<?php echo rand();?>" id='captchaimg' >
index.php
<?php
session_start();
//Settings: You can customize the captcha here
$image_width = 210;
$image_height = 60;
$characters_on_image = 7;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789ABDEFGHLMNQRTWY';
$random_dots = 15;
$random_lines = 3;
$captcha_text_color="#4593BC";
$captcha_noice_color = "#34495e";
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.6;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser window
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['captcha'] = $code;
function hexrgb ($hexstr){
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
?>
//to display captcha image
<img src="captcha.php?rand=<?php echo rand();?>" id='captchaimg' >
//显示验证码图像
“id='captchaimg'>
font-xeron.ttf的位置是什么?它与我的captcha.phpok位于同一位置。你是否尝试使用另一种.ttf字体?尝试将“xeron.ttf”更改为“./xeron.ttf”以明确你想要当前的工作目录。或者指定完整路径。错误日志显示了什么?仅供参考:@DrMJ是的,我尝试使用不同的.ttf文件。但仍然是没有加载。我已经编辑了一些部分…创建captcha.php文件并将其保存在根文件夹中,然后添加index.php的代码。请在我的whatsapp编号9881150272上戳我一下