1

我的验证码有以下代码。图像中的线条已生成,但未显示 4 位数字。我认为有一些问题

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);
    ?>
4

1 回答 1

1

下面的代码在我的电脑上完美运行

记住字体路径应该是正确的。

输出-在此处输入图像描述

<?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);
于 2012-08-05T09:13:30.517 回答