我想将一些文本打印到PNG文件中。我已经尝试了一切,但没有运气。以JPG格式显示文本
这里是我的代码:
<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 )。
感谢您的回复。我把它留在了原来的位置,因为我在页面的其余部分需要它(未包含在代码中)。然而,仅仅为了测试的目的,我把它从我的代码中移除了,并且我得到了不同的垃圾。 –
您无法从同一页面打印图像和html。作为对HTTP请求的响应,您必须发回一个特定的内容类型。 “我只是得到了不同的垃圾”:这实际上是生成的图像,将图像标记加载到图像标记中,您将看到,或者尝试在新的浏览器会话中打开它。 –
我提供的代码是当前我在页面上的所有内容(取出文本/ html内容类型)。当你说加载到图像标签的URL,你的意思是在我的网页上有其中barcode.php是上面提供的代码? –