PHP代码不能用于创建验证码


the php code is not working for creating captcha

我试图在php中显示一些随机数作为图像。实际上,我在我的方式开发验证码在php,我知道这些只是随机数,但首先我认为我应该做一个基本的随机数图像,所以我寻找它,并创建了下面的代码,但它不工作,它不显示任何东西,除了所有的文本,我把在img标签。有人可以指出什么错误在给定的代码。Thanyou ! !

generator.php

<?php
session_start();
header("Content-type: image/jpeg");
$text=$_SESSION['text']=rand(10000,99999);
$image_width= 100;
$image_height= 40;
$font_size= 30;
$image=imagecreate($image_width,$image_height);
imagecolorallocate($image,255,255,255);
$text_color=imagecolorallocate($image,0,0,0);
imagettftext($image,$font_size,0,15,30,$text_color,'Sans-serif',$text);
imagejpeg($image);
?>

captcha.php

<?php
?>
<html>
<head>
</head>
<body>
<h1>Captcha PHP </h1>
<img src="generator.php" alt="There is something wrong"/>
</body>
</html>

这里的问题是您没有可供加载的字体。

你应该看到:

警告:imagettftext(): Could not find/open font in…

作为一个通知,因为它在测试你的代码时为我做了。如果没有显示,请检查日志。

从任何流行的字体网站下载"Sans-serif"字体或"Arial"字体。

首先声明为:

$font = 'arial.ttf'; // I used this as an example

然后将现有行修改为:

imagettftext($image,$font_size,0,15,30,$text_color,$font,$text);

旁注:你可能需要增加图像的宽度,或者减小字体大小,因为它确实在右边截断了一点。

我在这里找到了"Arial.ttf"文件进行测试:

http://www.font-police.com/affiche.php?a=classique& b = sans-serif& c = arial.ttf

直接链接:

  • http://www.font-police.com/classique/sans-serif/arial.ttf

下载它(用于测试)并将其上传到服务器,以便它与您的.php文件位于同一文件夹中。

注意::这些是示例链接,并在此发布时工作。

如果它们将来不再存在,只需寻找其他字体来源,那里有很多。