这个代码出了什么问题?(imagettftext)等等


What is wrong with this code? (imagettftext)and like that

Hallo所以这就是我所拥有的:

test.php

<form action="index.php" method="post">
   <input type="text" name="nick">
   <input type="submit" name="test">
</form>

index.php

    <?php
if(isset($_POST['test'])) {
  $bg = imagecreatefrompng('img/sygn/1.png') or die("t");
    $font = "others/rte.ttf";
    $blackColor = imagecolorallocate($bg, 47, 53, 62);
    $diamondColor = imagecolorallocate($bg, 140, 244, 226);
    $emeraldColor = imagecolorallocate($bg, 92, 244, 149);
    $goldCollor = imagecolorallocate($bg, 234, 238, 87);
    header("Content-type: image/png");
    imagettftext($bg, 29, 0, 5, 33, $blackColor, $font, 'Username');
    imagettftext($bg, 17, 0, 360, 83, $goldCollor, $font, '20');
    imagettftext($bg, 17, 0, 360, 43, $emeraldColor, $font, '20');
    imagettftext($bg, 28, 0, 50, 125, $diamondColor, $font, '400');
    imagepng($bg);
    imagepng($bg, "users/image.png");
    imagedestroy($bg);
}
?>

我想在点击按钮后显示图像。但它不起作用,我没有得到任何图像,只有空白的一面。如果我删除If语句,它工作正常。如果我添加:

echo '<input type="text">'

在第一行中,图像生成器无法正常工作。与示例相同:

include form.php

这就是我得到的:

http://screenshot.net/pjg6du1

试试这个:

if(isset($_POST['nick']) && $_POST['nick'] != '') {
    //my code
}

您需要为您的输入提交设置如下值:

<input type="submit" value="Submit" name="submit" />

类似的另一个答案

  1. 检查所有路径是否正确(img、字体和用户目录)
  2. 你的if语句是正确的
  3. 在您的页面上添加
  4. 它有效

imagepng在用户目录中创建图像,但不会在页面上显示。

下面的正确代码:

<html>
<head>
<?php
if(isset($_POST['test'])) {
  $bg = imagecreatefrompng('img/sygn/1.png') or die("t");
    $font = "others/rte.ttf";
    $blackColor = imagecolorallocate($bg, 47, 53, 62);
    $diamondColor = imagecolorallocate($bg, 140, 244, 226);
    $emeraldColor = imagecolorallocate($bg, 92, 244, 149);
    $goldCollor = imagecolorallocate($bg, 234, 238, 87);
    header("Content-type: image/png");
    imagettftext($bg, 29, 0, 5, 33, $blackColor, $font, 'Username');
    imagettftext($bg, 17, 0, 360, 83, $goldCollor, $font, '20');
    imagettftext($bg, 17, 0, 360, 43, $emeraldColor, $font, '20');
    imagettftext($bg, 28, 0, 50, 125, $diamondColor, $font, '400');
    imagepng($bg);
    imagepng($bg, "users/image.png");
    imagedestroy($bg);
}
?>
</head>
<body>
<form action="test.php" method="post">
   <input type="text" name="nick">
   <input type="submit" name="test">
</form>
<img src="users/image.png" />
</body>
</html>