1

我编写了这个函数来从文本创建 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`�

怎么了?

4

3 回答 3

2

将标题设置在您知道输出将是图像的位置。这意味着设置此语句

header('Content-type: image/png');

在您的 php 脚本的开头。

也有可能在此之前已经执行了 header-command。

于 2012-08-11T14:14:35.397 回答
1

header('Content-type: image/png');putob_clean(); 清理响应对象之前,您可以再次添加标头

于 2012-08-11T14:36:56.167 回答
1

您不能在页面中使用多个标题。

于 2013-02-05T09:46:12.730 回答