Cannot modify header information - headers already sent by [duplicate]
<?php
require_once 'wordwrap.php';
$text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum";
$im=imagecreatefrompng('testing.png');
$arr=word($text);
$white = imagecolorallocate($im,255,255,255);
$grey = imagecolorallocate($im, 128, 128, 128);
$font='arial.ttf';
$m=121;
for($i=0;$i<sizeof($arr);$i++)
{
//imagettftext($im,12,0,11,$m+$t,$grey,$font,$arr[$i]);
//echo $arr[$i]."<br/>";
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
I am getting error.
Warning: Cannot modify header information - headers already sent by (output started at /home2/puneetbh/public_html/prideapp/Testing/wordwrap.php:33) in /home2/puneetbh/public_html/prideapp/Testing/checkimage.php on line 16
‰PNG ��� IHDR�� ��ô���J"Þ/�� �IDATxœì¼KvÉŽ%ŠŸ}Ü)EfäjÕxÝËÇ›nE^)Èãn?�Õ€J‘UÕ~ß --ŠKä>ƒÛàÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ7nܸqãÆ
your wordwrap.php file should not output a byte. But it prints something out in the 33th line. You can check this line and see what happened.
Also consider to use bulit-in PHP function wordwrap() instead of including some strange code.
So, make it like this:
<?php
#require_once 'wordwrap.php';
$text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum";
$im=imagecreatefrompng('testing.png');
$arr=explode("\n",wordwrap($text,24,"\n"));
$white = imagecolorallocate($im,255,255,255);
$grey = imagecolorallocate($im, 128, 128, 128);
$font='arial.ttf';
$m=121;
for($i=0;$i<sizeof($arr);$i++)
{
//imagettftext($im,12,0,11,$m+$t,$grey,$font,$arr[$i]);
//echo $arr[$i]."<br/>";
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Make sure there are no spaces or returns before <?php
.
Also if your file is encoded as UTF-8 make sure it's without BOM
turn on output buffering for changing header -> http://php.net/manual/en/function.ob-start.php
write this to top of your code.
ob_start();
精彩评论