douyuliu9527 2018-06-19 04:38
浏览 128

没有显示文字,只有验证码线可见

PHP code:

<?php 
ob_start();
session_start();
$_SESSION['secure'];
header('content-type:image/png');
$text="hello";
$font_size=25;
$width=200;
$height=200;
$image=imagecreate($width, $height);
imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
for($x=1; $x <= 30; $x++) {
    $x1 = rand(1, 100);
    $y1 = rand(1, 100);
    $x2 = rand(1, 100);
    $y2 = rand(1, 100);
    imageline($image, $x1, $y1, $x2, $y2, $black);
}
imagettftext($image, $font_size, 0, 15, 30, $black, 'consolas.ttf', $text);
$imgSrc="out.png";
imagejpeg($image, $imgSrc);
?>

When the image is generated, only lines are drawn but text("hello") is not drawn on it. The font is also in the same directory, I have also kept it in another directory and added the complete path no luck.

  • 写回答

1条回答 默认 最新

  • dongsi2317 2018-06-19 05:37
    关注

    I'm debugging this issue and found that font file is not accessible.

    I have checked by turning on debugging as below:

    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    

    And commented lines below.

    header('content-type:image/png');
    
    
    imagepng($image);
    

    Got error:

    Warning: imagettftext(): Could not find/open font in /var/www/html/index.php on line 24

    Soultion:

    Path to font file should be realpath on server. So the code should be:

    imagettftext($image, $font_size, 0, 15, 30, $black, realpath('consolas.ttf'), $text);
    

    Also change in render image without giving $imgSrc and changing it to

    imagepng($image);
    

    instead of

    $imgSrc="out.png";
    imagejpeg($image, $imgSrc);
    

    Finally complete code will look like:

    ob_start();
    session_start();
    $_SESSION['secure'];
    header('content-type:image/png');
    $text = "hello";
    $font_size = 25;
    $width = 200;
    $height = 200;
    $image = imagecreate($width, $height);
    imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);
    for ($x = 1; $x <= 30; $x++) {
        $x1 = rand(1, 100);
        $y1 = rand(1, 100);
        $x2 = rand(1, 100);
        $y2 = rand(1, 100);
        imageline($image, $x1, $y1, $x2, $y2, $black);
    }
    imagettftext($image, $font_size, 0, 15, 30, $black, realpath('Consolas.ttf'), $text);
    imagepng($image);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计