簡體   English   中英

imagettftext函數在PHP中不起作用

[英]imagettftext function not working in php

我的驗證碼有以下代碼。 圖像中的線條已生成,但不顯示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);
    ?>

下面的代碼完美地在我的電腦上工作

記住字體路徑應該正確。

輸出- 在此處輸入圖片說明

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM