Php 无法在图像上显示文本
我想在图像上显示一些文字。我正在使用以下代码:Php 无法在图像上显示文本,php,gd,Php,Gd,我想在图像上显示一些文字。我正在使用以下代码: 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(su
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
,此图像上不显示文本?有什么问题吗
$\u会话['name']是我登录facebook并将用户名保存到$\u会话时的值
我已经在服务器上上传了上述代码:
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不以前导/then开头。ttf将附加到
文件名和库将尝试搜索该文件名
沿着库定义的字体路径
在许多情况下,字体与脚本位于同一目录中
使用它,下面的技巧将缓解任何include问题
检查GD和图像功能是否启用?字体的路径可能不合适-尝试使用realpath()和字体的绝对路径location@JasbirSinghSohanpal如何检查这个函数?@JasbirSinghSohanpal如果GD被禁用,那么我认为图像也不应该被看到。使用phpinfo()检查它;功能。还有一件事我想问,如果我想在facebook上发布这张图片,那么我该如何发布?我想这取决于上面的代码是如何使用的。如果这是一个名为“love.php”的脚本,您在php/html中使用它,如
等,那么我将尝试在服务器上的.htaccess文件中添加一个条目,将其重命名为love.jpg等,然后发布该文件的完整链接,如http://rohitashvsinghal.com/love.jpg
<?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';
?>