2013-10-30 56 views
1

我想将一些文本打印到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 )。

回答

1

为什么要发送两个内容类型标题?取下第一个它说,它的text/html

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

只有这样的Content-Type应该留你的形象

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

感谢您的回复。我把它留在了原来的位置,因为我在页面的其余部分需要它(未包含在代码中)。然而,仅仅为了测试的目的,我把它从我的代码中移除了,并且我得到了不同的垃圾。 –

+0

您无法从同一页面打印图像和html。作为对HTTP请求的响应,您必须发回一个特定的内容类型。 “我只是得到了不同的垃圾”:这实际上是生成的图像,将图像标记加载到图像标记中,您将看到,或者尝试在新的浏览器会话中打开它。 –

+0

我提供的代码是当前我在页面上的所有内容(取出文本/ html内容类型)。当你说加载到图像标签的URL,你的意思是在我的网页上有其中barcode.php是上面提供的代码? –