无法在图像上显示文本



我想在图像上显示一些文本。我使用以下代码:

session_start();
$name = $_SESSION['name'];
$nameLength = strlen($name); // gets the length of the name
$randomNumber = rand(0, $nameLength - 1); // generates a random number no longer than the name length
$string = ucfirst(substr($name, $randomNumber, 1)); // gets the substring of one letter based on that random number
$im = imagecreatefromjpeg("love.jpg");
$text = "  First Letter of Your partner's name is";
$font = "Font.ttf";
$black = imagecolorallocate($im, 0, 0, 0);
$black1 = imagecolorallocate($im, 255, 0, 0);
imagettftext($im, 32, 0, 380, 430, $black, $font, $text);
imagettftext($im, 52, 0, 630, 530, $black1, $font, $string);
imagejpeg($im, null, 90);

但是在localhost上,这段代码显示了图像上的文本,但是当我将其上传到服务器时,它只显示了image,文本没有显示在这个图像上?有什么问题吗?

$_SESSION['name'] is value when I login to facebook and save the name of the user into $_SESSION

我已经把上面的代码上传到服务器:

http://rohitashvsinghal.com/fb/login.php

字体必须存在于明显指定的位置,以我的经验,字体的全路径效果最好。

<?php
    session_start();
    $name = $_SESSION['name'];
    $nameLength = strlen($name);
    $randomNumber = rand(0, $nameLength - 1);
    $string = ucfirst(substr($name, $randomNumber, 1));
    $im = imagecreatefromjpeg("love.jpg");
    $text = "  First Letter of Your partner's name is";
    /* Either use the fullpath or the method given below from the manual */
    $fontpath=$_SERVER['DOCUMENT_ROOT'].'/path/to/fonts/';
    putenv('GDFONTPATH='.realpath( $fontpath ) );
    $fontname='arial.ttf';
    $font = realpath( $fontpath . DIRECTORY_SEPARATOR . $fontname );

    $black = imagecolorallocate($im, 0, 0, 0);
    $black1 = imagecolorallocate($im, 255, 0, 0);
    imagettftext($im, 32, 0, 380, 430, $black, $font, $text);
    imagettftext($im, 52, 0, 630, 530, $black1, $font, $string);
    imagejpeg($im, null, 90);
?>

但是,手册说:-

根据PHP使用的GD库的版本,何时Fontfile不以前导/开始,然后将追加.ttf文件名和库将尝试搜索该文件名沿着库定义的字体路径。

在许多情况下,字体与脚本位于同一目录使用下面的技巧可以缓解任何include问题。

<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>

最新更新