title: Symfony2: Spaces in image when using GD
body: <p>I'm currently working on a project where I have to render barcodes to print them. To do so, I use a code I got online which works fine if I use it in a separate file. The only problem is I have to use it with symfony 2.1. </p>
  
  <p>This script uses GD to draw a barcode. The problem is that when I try to display the image with the imagepng function, I have an error. After trying to download the png as an attachement file, I noticed that there is 4 spaces before the binary data in the png file. It starts like that : </p>
  
  <pre><code>    ‰PNG
  </code></pre>
  
  <p>If I remove the spaces manually, the image is displayed but I need to display the png directly in the browser.</p>
  
  <p>Do you have an idea as to why the spaces are prepended to the image file?</p>
  
  <p>Here is the code used to generate the png.</p>
  
  <pre><code> public function barcodeAction($text) {
      $response = new Response();
      $response-&gt;headers-&gt;set('Content-Type', 'image/png');
      $response-&gt;headers-&gt;set('Content-Disposition', sprintf('attachment; filename="%s.png"', $text));
      $size = "40";
      $code_string = "";
  
      $chksum = 104;
  
      $code_array = array(" " =&gt; "212222", "!" =&gt; "222122", "\"" =&gt; "222221", "#" =&gt; "121223", "$" =&gt; "121322", "%" =&gt; "131222", "&amp;" =&gt; "122213", "'" =&gt; "122312", "(" =&gt; "132212", ")" =&gt; "221213", "*" =&gt; "221312", "+" =&gt; "231212", "," =&gt; "112232", "-" =&gt; "122132", "." =&gt; "122231", "/" =&gt; "113222", "0" =&gt; "123122", "1" =&gt; "123221", "2" =&gt; "223211", "3" =&gt; "221132", "4" =&gt; "221231", "5" =&gt; "213212", "6" =&gt; "223112", "7" =&gt; "312131", "8" =&gt; "311222", "9" =&gt; "321122", ":" =&gt; "321221", ";" =&gt; "312212", "&lt;" =&gt; "322112", "=" =&gt; "322211", "&gt;" =&gt; "212123", "?" =&gt; "212321", "@" =&gt; "232121", "A" =&gt; "111323", "B" =&gt; "131123", "C" =&gt; "131321", "D" =&gt; "112313", "E" =&gt; "132113", "F" =&gt; "132311", "G" =&gt; "211313", "H" =&gt; "231113", "I" =&gt; "231311", "J" =&gt; "112133", "K" =&gt; "112331", "L" =&gt; "132131", "M" =&gt; "113123", "N" =&gt; "113321", "O" =&gt; "133121", "P" =&gt; "313121", "Q" =&gt; "211331", "R" =&gt; "231131", "S" =&gt; "213113", "T" =&gt; "213311", "U" =&gt; "213131", "V" =&gt; "311123", "W" =&gt; "311321", "X" =&gt; "331121", "Y" =&gt; "312113", "Z" =&gt; "312311", "[" =&gt; "332111", "\\" =&gt; "314111", "]" =&gt; "221411", "^" =&gt; "431111", "_" =&gt; "111224", "\`" =&gt; "111422", "a" =&gt; "121124", "b" =&gt; "121421", "c" =&gt; "141122", "d" =&gt; "141221", "e" =&gt; "112214", "f" =&gt; "112412", "g" =&gt; "122114", "h" =&gt; "122411", "i" =&gt; "142112", "j" =&gt; "142211", "k" =&gt; "241211", "l" =&gt; "221114", "m" =&gt; "413111", "n" =&gt; "241112", "o" =&gt; "134111", "p" =&gt; "111242", "q" =&gt; "121142", "r" =&gt; "121241", "s" =&gt; "114212", "t" =&gt; "124112", "u" =&gt; "124211", "v" =&gt; "411212", "w" =&gt; "421112", "x" =&gt; "421211", "y" =&gt; "212141", "z" =&gt; "214121", "{" =&gt; "412121", "|" =&gt; "111143", "}" =&gt; "111341", "~" =&gt; "131141", "DEL" =&gt; "114113", "FNC 3" =&gt; "114311", "FNC 2" =&gt; "411113", "SHIFT" =&gt; "411311", "CODE C" =&gt; "113141", "FNC 4" =&gt; "114131", "CODE A" =&gt; "311141", "FNC 1" =&gt; "411131", "Start A" =&gt; "211412", "Start B" =&gt; "211214", "Start C" =&gt; "211232", "Stop" =&gt; "2331112");
      $code_keys = array_keys($code_array);
      $code_values = array_flip($code_keys);
      for ($X = 1; $X &lt;= strlen($text); $X++) {
          $activeKey = substr($text, ($X - 1), 1);
          $code_string .= $code_array[$activeKey];
          $chksum = ($chksum + ($code_values[$activeKey] * $X));
      }
      $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
  
      $code_string = "211214" . $code_string . "2331112";
  
      // Pad the edges of the barcode
      $code_length = 10;
      for ($i = 1; $i &lt;= strlen($code_string); $i++) {
          $code_length = $code_length + (integer) (substr($code_string, ($i - 1), 1));
      }
      $img_width = $code_length;
      $img_height = $size;
  
      $image = imagecreate($img_width + 10, $img_height);
      $black = imagecolorallocate($image, 0, 0, 0);
      $white = imagecolorallocate($image, 255, 255, 255);
  
      imagefill($image, 0, 0, $white);
  
      $location = 10;
      for ($position = 1; $position &lt;= strlen($code_string); $position++) {
          $cur_size = $location + (substr($code_string, ($position - 1), 1) );
          imagefilledrectangle($image, $location, 0, $cur_size, $img_height, ($position % 2 == 0 ? $white : $black));
          $location = $cur_size;
      }
      imagepng($image);
      imagedestroy($image);
      return $response;
  }
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p><hr />
  <b>Edit:</b>
  The problem is solved.
  For that, I'd like to thank both Diego Agulló and hacfi.<br /><br />
  Of course, I checked before posting if there were any leading whitespaces in the php file containing the function so I couldn't figure out the origin of the problem. The last message of hacfi made me realise that the spaces could come from a file that was send before the current file. 
  I discovered the linebreaks in the files I use to authenticate my users (the authentification provider and listener).<br /></p>
  
  <p>So, again, thank you.</p>