【发布时间】:2016-05-14 10:45:55
【问题描述】:
您好,关注此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() 函数我得到以下信息:
对可能发生的事情有什么想法吗?
【问题讨论】:
-
$font_path存在吗?使用file_exists()进行检查。 -
是的,运行 if(!file_exists($font_path)) { die("No font"); },什么也没返回。所以我相信该文件确实存在。
-
serra.jpg的尺寸是多少?您确定文本的坐标不在图像之外吗? -
serra.jpg 是 2272x1704,我确实玩过文本的位置,看看是否是问题所在,但没有运气。
-
@André 你检查了吗imagetext