我正在尝试在图像上写入文本并在浏览器上显示该图像。我尝试下面的代码,但它无法正常工作。下面的代码显示了一个空方块(没有图像)。谁能说出我在这里做错了什么呢。
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('images/travel.jpg');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$text = "This is a sunset!";
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
?>
答案 0 :(得分:2)
代码中的问题是这一行
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
正如文档http://www.php.net/manual/en/function.imagettftext.php中所述 你也必须传递字体文件路径
所以只需更改行
imagettftext($jpg_image, 15, 0, 15, 15, $white, 'Roboto-Bold.ttf',$text);
并将一个字体文件放到该文件夹中,它将起作用。
如果您不想让字体文件替换
行imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
用
imagestring($jpg_image,15,15,15,$text,$white);
您的代码将是
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQf66Ol6NAa6sdNhDJT0z1fVfTmjPjxAHkopPwExZ9AqHSqNzHP');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$text = "This is a sunset!";
imagestring($jpg_image,15,15,15,$text,$white);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
?>
这对你有用