使用字体PHP时捕获图像生成问题


Captcha image generating problems when using font PHP

在我尝试使用文本字体之前,它的工作非常完美。

这是我的密码。。现在评论的3er行是一个很好的工作。。。但在这个例子中,我试图用imagettftext替换这一行。。运气不好。

我的错误是什么?

$newImage = imagecreatefromjpeg( "orsil_secure.jpg" ); 
$txtColor = imagecolorallocate($newImage, 0, 0, 0); 
//imagestring($newImage, 5, 10, 27, $ranStr,  $txtColor); 
imagettftext($newImage, 5, 10, 27, $txtColor, $font_path, $ranStr);
header( "Content-type: image/jpeg" ); 
imagejpeg($newImage); 

哦,是的,在前几行是到字体的路线在这里:

// random number
$ranStr = md5(microtime()); 
$ranStr = substr($ranStr, 0, 6); 
// Set Path to Font File
  $font_path = 'captcha.TTF';

对imagettftext的调用中似乎有错误的参数。

据推测,5对应于imagestring中使用的字体,但在imagettftext中没有位置。您还需要指定尺寸和角度。

例如:

imagettftext($newImage, 12, 45, 10, 27, $txtColor, $font_path, $ranStr);
                        ^^  ^^
                        ||  ||
                        ||   ------ angle
                        ----------- size

在本例中:

size=12px或12pt,具体取决于您使用的是GD1还是GD2

角度=45°

很明显,你会想在这里使用你自己的价值观。

如果它仍然不起作用,那么很可能是字体文件的路径错误。检查它是否与PHP脚本位于同一文件夹中。

我认为imagettftext中缺少一个参数。如果你看http://php.net/manual/en/function.imagettftext.php它具有sizeanglexy
你错过了4个(我猜是angle)中的一个。

所以它需要像这样:

imagettftext($newImage, 5, 0, 10, 27, $txtColor, $font_path, $ranStr);

其中0表示angle