您好,关注此answer,我试图使用GD库向图像添加文字,遗憾的是我无法这样做,这是我的代码:
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('serra.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'denmark.ttf';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
此代码将输出没有文字的图片。
serra.jpg和denmark.ttf文件与脚本位于同一文件夹中。
在我的服务器上运行gd_info()函数我得到以下内容:
关于可能发生什么的任何想法?
答案 0 :(得分:1)
执行以下步骤:
'denmark.ttf'
个文件。确保您的php脚本可以访问它$font_path = './denmark.ttf';
答案 1 :(得分:0)
获取解决方案,检查此代码。
imagejpeg($jpg_image,'newimage.jpeg');
您无法保存图片。
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$source="mk.jpg";
$jpg_image = imagecreatefromjpeg($source);
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'ASMAN.ttf';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image,'newimage.jpeg');
// Clear Memory
imagedestroy($jpg_image);
?>
答案 2 :(得分:0)
我在$ font_path上遇到了一些奇怪的问题。
阅读http://php.net/manual/en/function.imagettftext.php
我看到我必须在$ font_path变量之前添加putenv ...,如下所示:
putenv('GDFONTPATH=' . realpath('.'));
$font_path = 'denmark.ttf';
现在工作正常。
此外,我测试了Roman提供的解决方案,只需将font_path更改为:
$font_path = './denmark.ttf';
脚本运行正常!
感谢帮助人员!
答案 3 :(得分:0)
$font_path = 'denmark.ttf';
应该是
$font_path = 'denmark'; //no need to incluce .ttf when not using backward slash.