我写了这个函数来从文本创建png文件:
function pngfromtext($text){
$fontsize = 5;
$width = imagefontwidth($fontsize)* strlen($text);
$height = imagefontheight($fontsize);
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 255, 255);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
}
我将代码放入functions.php
文件中,在另一个页面中使用此功能时,出现此错误:
Warning: Cannot modify header information - headers already sent by (output started at ..'functions.php on line 58
�PNG IHDRZ^%JPLTE����ٟ�tRNS@��f�IDAT�c` Hȱ�7�H��'��`c��s�����i��$���Hl`8��Ɛ�� ��#�c��p�� q�3f�òm�� �g�ـ�6fF ���h�bc�sXd4c�A4����?|�¦����r+���!IEND�B`�
怎么了?
将标题设置在你知道输出将是图像的位置。这意味着设置此语句
header('Content-type: image/png');
在 PHP 脚本的开头。
也有可能在这一点之前已经执行了标头命令。
您不能在页面上使用多个标题。
在header('Content-type: image/png');
放ob_clean();
之前这将清理响应对象,以便您可以再次添加标头