2

它工作完美,直到我尝试使用文本字体。

这是我的代码.. 现在注释的 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';
4

2 回答 2

1

我认为您在imagettftext. 如果您查看http://php.net/manual/en/function.imagettftext.php它有size,angle和. 您缺少 4 个中的一个(我猜是)。xy
angle

所以它需要是这样的:

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

其中 0 代表angle.

于 2013-07-27T16:36:58.060 回答
1

看起来您在调用imagettftext时有错误的参数。

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

例如:

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

在这个例子中:

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

角度 = 45°

显然,你会想在这里使用你自己的值。

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

于 2013-07-27T17:25:23.563 回答