drqja5919276 2018-10-06 09:29
浏览 327

使用GD库进行验证码,不显示图像

I am using a GD library to generate captcha image. But the image is not showing up. Can you please help me with it. This is a text converted to captcha image index.php

<?php
session_start();
$_SESSION['secure']=rand(1000,9999);
?>

<img src="generate.php" />

generate.php

<?php 
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size=30;

$image_width=200;
$image_height=40;

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

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

?>

These above are the two files with which I am trying to generate a captcha image. But the image is not showing up. My GD library is enabled, i have had a check on it. Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • douqiao3453 2018-10-06 11:35
    关注

    From documentation

    Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

    So try something like:

    $font = "/path_to_folder/font.ttf";

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

    This work for me.

    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试