【问题标题】:how to change font family of captcha and give image in background of captcha?如何更改验证码的字体系列并在验证码的背景中提供图像?
【发布时间】:2026-01-28 19:10:01
【问题描述】:

如何更改验证码的字体系列并在验证码的背景中提供图像,请告诉我是否有人知道。代码如下:

<?php 
    session_start();
    $change_time = md5(microtime());
    $get_value = substr($change_time, 0, 6);
    $_SESSION['value'] = $get_value;
    $create_image  = imagecreate(100, 30);
    imagecolorallocate($create_image, 51, 112, 183);
    $text_color  = imagecolorallocate($create_image, 255, 255, 255);
    imagestring($create_image, 50, 15, 7, $get_value, $text_color);
    header('Content:image/jpeg');
    imagejpeg($create_image);
?>

【问题讨论】:

    标签: javascript php jquery wordpress


    【解决方案1】:

    看看:


    我这样使用它(包含的额外文件):

    session_name("page_session_name");
    session_start();
    
    header("Content-Type: image/png");      // Sets the Content of this file to an PNG-Image
    
    $ttf = "./franconi.ttf";     // font path
    $_SESSION["captcha_id"] = "";          // Clears the old value
    $rnd = mt_rand(30000,90000);    // Generates a random number between 30000 and 90000
    
    $_SESSION["captcha_id"] = $rnd;   // Sets the value of the session
    $bild = imagecreatefromgif("bg.gif");     // Creates a new image from background file
    $textColor = imagecolorallocate($bild, 255, 255, 255);              // Sets white text color
    imagettftext($bild, 11, 10, 5, 20, $textColor, $ttf, $_SESSION["captcha_id"]); 
    
    ImagePNG($bild);             // Output for the generated image
    

    像这样包含它:

    <img src="./path_to/captcha.php" title="Captcha" height="50" width="100"/>
    

    【讨论】: