title: PHP GD header issue
body: <p>A couple of months ago I enabled GD on localhost to play around with it a bit, used various scripts found online to get a better understanding of what it does, all good and well.</p>
  
  <p>But now I feel like I'm descending into insanity here because when trying to repeat the above with the exact same scripts I keep getting header errors, both on local and remote host. I figure I must have somehow completely forgotten how do do it right so I need a brief refresher on this.</p>
  
  <p>For example, this was the first script I ever tried:</p>
  
  <pre><code>$my_img = imagecreate( 200, 80 );
  $background = imagecolorallocate( $my_img, 0, 0, 255 );
  $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
  $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
  imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
  $text_colour );
  imagesetthickness ( $my_img, 5 );
  imageline( $my_img, 30, 45, 165, 45, $line_colour );
  
  header( "Content-type: image/png" );
  imagepng( $my_img );
  imagecolordeallocate( $line_color );
  imagecolordeallocate( $text_color );
  imagecolordeallocate( $background );
  imagedestroy( $my_img );
  </code></pre>
  
  <p><a href="http://www.thesitewizard.com/php/create-image.shtml" rel="nofollow noreferrer">Source</a></p>
  
  <p>Worked fine back then, now when I run it I get this:</p>
  
  <p>Warning: Cannot modify header information - headers already sent by (output started at H:\xampp\htdocs\tests\script.php:1) in H:\xampp\htdocs\tests\script.php  on line 11
  �PNG  ��� IHDR�������P���!�#��� PLTE���������M�)B����IDATH��1�0EA��N�����:��8Q�JN�d�Z��<code>��! � ���k�Z⊜���k��g�˕��|K$�M��|)��$ů8DfRQB���-��[I��tźR�+���%�"��Ut)(�K�@dF�e�zKF9&amp;�g|C7i��d��\G�Y � �3y�Ƌ�_�%�����IEND�B</code>�</p>
  
  <p>Warning: Wrong parameter count for imagecolordeallocate() in H:\xampp\htdocs\tests\script.php on line 13</p>
  
  <p>Warning: Wrong parameter count for imagecolordeallocate() in H:\xampp\htdocs\tests\script.php on line 14</p>
  
  <p>Warning: Wrong parameter count for imagecolordeallocate() in H:\xampp\htdocs\tests\script.php on line 15</p>
  
  <p>Same thing for the second script I ever tried:</p>
  
  <pre><code>// create a 200*200 image
  $img = imagecreatetruecolor(200, 200);
  
  // allocate some colors
  $white = imagecolorallocate($img, 255, 255, 255);
  $red   = imagecolorallocate($img, 255,   0,   0);
  $green = imagecolorallocate($img,   0, 255,   0);
  $blue  = imagecolorallocate($img,   0,   0, 255);
  
  // draw the head
  imagearc($img, 100, 100, 200, 200,  0, 360, $white);
  // mouth
  imagearc($img, 100, 100, 150, 150, 25, 155, $red);
  // left and then the right eye
  imagearc($img,  60,  75,  50,  50,  0, 360, $green);
  imagearc($img, 140,  75,  50,  50,  0, 360, $blue);
  
  // output image in the browser
  header("Content-type: image/png");
  imagepng($img);
  
  // free memory
  imagedestroy($img);
  </code></pre>
  
  <p><a href="http://in.php.net/manual/en/function.imagearc.php" rel="nofollow noreferrer">Source</a></p>
  
  <p>That now outputs:</p>
  
  <p>Warning: Cannot modify header information - headers already sent by (output started at H:\xampp\htdocs\tests\script.php:1) in H:\xampp\htdocs\tests\script.php  on line 22
  �PNG  ��� IHDR�����������":9���gIDATx����v�H�a&lt;�4Ƹ���ܕ��:/{�v�}��z����C��(�~��҆�Uj1�jƖ�~)y[�x��JW��+/n�q�9)eɽ?�n�q%Pfg��f� �s+��I޽;I��l ��2s��-R����u 9���b�}��H�d�B�v��R��2��Kr�h�u�v�8���y�U��|��^O�T�'�:��I *R!��r�%���Z����$_�L�ʴVd�W�U�'��tk��2\�XA�5�s���ϔGL0�<em>Q���:0O|M�W� ����j�ܼC��</em>�U}�՚���FŹǂ�UO�֝���ά�OW�z�k���N"��� ���1F��"ls{r�?�@��/v���ZSw1i���? i�8�]��C��A��xN?xw�죙��ۮ�9 -&amp;�C�h�N���"r��B��!����<code>��j�Ǹ�_"ե&gt;�Љ�u��q����9ɫ�T��5�s0*���*xݣ&gt; �X!�w�����R�u �~�-O7�"�� &lt;��·v���K��i�;�Dv��c��P�q��h,)~C��w���</code>��9��&lt;��N�!&amp;h,���<code>����q�5���v&amp;h,���</code>���&amp;��k��X\'��7�l�F�.�-�B���<code>�Ƃ�ğ'� ^Ajw�1�X0��X"�*��z�.�&amp;"+|{��v/a+0�1o�i0G!l�~�o�+_�� �_��� 8�r|��F���k��!������}&gt;��Ǟ7Xbb=�]�Rd��1��f�d�}�Nـ�k�ۉ���[5�k��6��y��I��jc�~/��˭N�ڍ�� �s�����4L�X0Ac��4L�X0����;�a�Ƃ &amp;h,���</code>�Ƃ%�q�,�Nh,���<code>�Ƃ1���=�V�h,���</code>��,|c�?L,���<code>�Ƃn�pϮs&lt;&amp;��ơ"��bq2�tX��w�0��h��bb�^����4�¿�+���; �p1�\�_����ܶ�\o�Z�$����Yxz˟Q�O:��</code>"��8� �Ov���|���78 ���WԸZ�X����(p\=����? y�a:��N���ޚ�4��{ ~b��[S$�UO��ᡷ�a�=��[���=֩��Z_6����w��|�}�CBf"�K�$${��3�E^�vMϨc$ڻ��f�Pu+W�+��;{�L �6Dލ�w�G������J��۪��T�c�rl��n_J�E]�|��m����}���an>���Խ����IEND�B`�</p>
  
  <p>However, the third script I ever tried still works:</p>
  
  <pre><code>// read the post data
      $data = array('100','200','300','400','500','350','270');
      $x_fld = array('Sun','Mon','Tue','Wen','Thu','Fir','Sat');
      $max = 0;
      for ($i=0;$i&lt;7;$i++){
        if ($data[$i] &gt; $max)$max=$data[$i];  // find the largest data
      }
      $im = imagecreate(320,255); // width , height px
  
      $white = imagecolorallocate($im,255,255,255); // allocate some color from RGB components remeber Physics
      $black = imagecolorallocate($im,0,0,0);   //
      $red = imagecolorallocate($im,255,0,0);   //
      $green = imagecolorallocate($im,0,255,0); //
      $blue = imagecolorallocate($im,0,0,255);  //
      //
      // create background box
      //imagerectangle($im, 1, 1, 319, 239, $black);
      //draw X, Y Co-Ordinate
      imageline($im, 10, 5, 10, 230, $blue );
      imageline($im, 10, 230, 300, 230, $blue );
      //Print X, Y
      imagestring($im,3,15,5,"Students",$black);
      imagestring($im,3,280,240,"Days",$black);
      imagestring($im,5,100,50,"Simple Graph",$red);
       imagestring($im,5,125,75,"by Vijit",$green);
  
      // what next draw the bars
      $x = 15;    // bar x1 position
      $y = 230;    // bar $y1 position
      $x_width = 20;  // width of bars
      $y_ht = 0; // height of bars, will be calculated later
      // get into some meat now, cheese for vegetarians;
      for ($i=0;$i&lt;7;$i++){
        $y_ht = ($data[$i]/$max)* 100;    // no validation so check if $max = 0 later;
        imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
        imagestring( $im,2,$x-1,$y+1,$x_fld[$i],$black);
        imagestring( $im,2,$x-1,$y+10,$data[$i],$black);
        $x += ($x_width+20);   // 20 is diff between two bars;
  
      }
      imagejpeg( $im, "graph.jpeg", 90);
      imagedestroy($im);
      echo "&lt;img src='graph.jpeg'&gt;&lt;p&gt;&lt;/p&gt;"; 
  </code></pre>
  
  <p><a href="http://www.weberdev.com/get_example-4619.html" rel="nofollow noreferrer">Source</a></p>
  
  <p>It seems that the fact it doesn't output a header is why it works, though why the others used to work and now don't are beyond me, especially since the errors are also being replicated on an unrelated remote host, am I simply being clueless and calling the scripts erroneously? And before anybody asks, yes, I'm sure GD has been fully enabled, here are the relevant specs from phpinfo:</p>
  
  <p><strong>GD Support:   enabled<br>
  GD Version:     bundled (2.0.34 compatible)<br>
  FreeType Support:   enabled<br>
  FreeType Linkage:   with freetype<br>
  FreeType Version:   2.1.9<br>
  T1Lib Support:  enabled<br>
  GIF Read Support:   enabled<br>
  GIF Create Support:     enabled<br>
  JPG Support:    enabled<br>
  PNG Support:    enabled<br>
  WBMP Support:   enabled<br>
  XBM Support:    enabled</strong> </p>
  
  <p>Can someone please enlighten this befuddled girl? </p>