使用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.

douwu0882
douwu0882 有人请回答我被严重困扰
接近 2 年之前 回复
dongzipu7517
dongzipu7517 font.ttf在同一个文件夹中
接近 2 年之前 回复
dongwuwan5646
dongwuwan5646 font.ttf存在吗?尝试删除标题('Content-type:image/jpeg');声明并使用您的浏览器导航到generate.php,它应该可以帮助您找出问题所在。
接近 2 年之前 回复
doushan6692
doushan6692 可能重复转换GD输出到base64
接近 2 年之前 回复

1个回答



来自文档 </ p>


根据PHP使用的GD库的版本,当fontfile不以前导/ / .ttf开头时将是 附加到文件名,库将尝试沿库定义的字体路径搜索该文件名。</ p>
</ blockquote>

请尝试类似:</ p>

$ font =“/ path_to_folder / font.ttf”; </ p>
nn

imagettftext($ image,$ font_size,0,15,30,$ text_color,$ font,$ text ); </ p>

这项工作适合我。</ p>
</ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐