将文本显示为 JPG


Displaying Text as JPG

我正在尝试将一些文本打印到png文件中。我已经尝试了一切,但没有运气。

这是我的代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="image/jpeg" />
</head>
<body>
<?php
// Create the image
$im = imagecreatetruecolor(100, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'fonts/FRE3OF9X.TTF'; //HAVE CHECKED THIS AND FILE EXISTS
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);
?>
</body>
</html>

我尝试了各种字体文件,并通过更改文件名来检查是否找到了该文件,以查看是否出现错误。我需要在服务器上安装任何其他东西才能完成这项工作吗?

输出是乱码文本(例如 tuvwxyz)。

为什么要发送两个内容类型标头?删除第一个说其text/html

删除:<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

只有此内容类型应保留在您的图像中

<meta http-equiv="Content-Type" content="image/jpeg" />

您始终可以使用 base64 编码的字符串并将其显示为图像: 嵌入 Base64 图像