无法在图片上显示文字
unable to show text on the image
我想在图片上显示一些文字。我正在使用以下代码:
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);
?>
然而,手册指出:-
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.
In many cases where a font resides in the same directory as the script
using it the following trick will alleviate any include problems.
<?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';
?>
我想在图片上显示一些文字。我正在使用以下代码:
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);
?>
然而,手册指出:-
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.
In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.
<?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';
?>