2012-08-05 168 views
1

我对我的验证码有以下代码。生成图像中的线条,但不显示4位数字。我假设有一些问题imagettftext函数不能在php中工作

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

代码的功能。我尝试使用$ _server ['DOCUMENT_ROOT']设置字体文件的路径;但没有工作。

下面的代码:

 <?php 
    session_start(); 
    header('Content-type: image/jpeg'); 

    $text= $_SESSION['secure']; 

    $font_size =30; 
    $image_width= 110; 
    $image_height=40; 

    $image=imagecreate($image_width,$image_height); 
    imagecolorallocate($image, 255, 255, 255); 
    $text_color = imagecolorallocate($image, 0, 0, 0); 


    for($x=1; $x<=30; $x++){ 
     $x1 = rand(1,100); 
     $x2 = rand(1,100); 
     $y1 = rand(1,100); 
     $y2 = rand(1,100); 
    imageline($image, $x1, $x2, $y1, $y2, $text_color); 
    } 

    imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text); 
    imagejpeg($image); 
    ?> 

回答

1

下面的代码在我的电脑

记住字体路径应该是正确的工作完美。

输出 - enter image description here

<?php 

// Set the content-type 
header('Content-type: image/png'); 

// Create the image 
$im = imagecreatetruecolor(400, 30); 

// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 128, 128, 128); 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 399, 29, $white); 

// The text to draw 
$text = 'Testing...'; 
// Replace path by your own font path 
$font = 'arial.ttf'; 

// Add some shadow to the text 
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 

// Add the text 
imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 

// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($im); 
imagedestroy($im); 
+0

对不起,我不明白你实际上是想说什么? – user1460822 2012-08-05 09:18:43

+0

看到这个网址http://stackoverflow.com/questions/8496809/why-isnt-the-php-imagettftext-function-working-in-this-case – 2012-08-05 09:19:51

+0

看到这两个网址http://stackoverflow.com/questions/ 7034698/imagettftext-not-working – 2012-08-05 09:21:33