title: How do I find out what the error is when Firefox says "the Image cannot be displayed because it contains errors"?
body: <p>I have a PHP file that makes an image. I have PHP configured to report errors on the screen. When I run the file in the PHP file in Firefox 23, it returns <code>the Image cannot be displayed because it contains errors</code>.</p>
  <p>How can I find out what particular error is at fault in the image?</p>
  <p>Here is the code:</p>
  <pre><code>&lt;?php
      $tfont[] = 'cylburn-webfont.ttf';
  
      $cryptinstall = &quot;./cryptographp.fct.php&quot;;
      include $cryptinstall;
  
      error_reporting(E_ALL ^ E_NOTICE);
      srand((double)microtime()*1000000);
  
      if ($_GET[$_GET['sn']] == &quot;&quot;)
          unset ($_GET['sn']);
  
      session_start();
  
      include('getcaptcha.php');
      $question = getcaptcha();
      $questionLen = strlen($question);
      $cryptwidth  = 25 * $questionLen;
  
      $cryptinstall2 = &quot;./cryptographp.cfg.php&quot;;
      include $cryptinstall;
  
      $cryptwidth = 900;
      $cryptheight = 200;
  
      // CrÈation du cryptogramme temporaire
      $imgtmp = imagecreatetruecolor($cryptwidth, $cryptheight);
      $blank  = imagecolorallocate($imgtmp, 255, 255, 255);
      $black   = imagecolorallocate($imgtmp, 0, 0, 0);
      imagefill($imgtmp, 0, 0, $blank);
  
      $word = '';
      $x = 10;
      $pair = rand(0, 1);
      $charnb = rand($charnbmin, $charnbmax);
  
  
      for ($i=0; $i&lt;= $questionLen; $i++) {
           $tword[$i]['font'] =  $tfont[array_rand($tfont, 1)];
           $tword[$i]['angle'] = (rand(1, 2) == 1) ? rand(0, $charanglemax) : rand(360-$charanglemax, 360);
  
           $tword[$i]['element'] = str_split($question)[$i-1];
  
           $tword[$i]['size'] = rand($charsizemin, $charsizemax);
           $tword[$i]['y'] = ($charup ? ($cryptheight/2) + rand(0, ($cryptheight/5)) : ($cryptheight/1.5));
           $word .= $tword[$i]['element'];
  
           $lafont = &quot;fonts/&quot; . $tword[$i]['font'];
           imagettftext($imgtmp, $tword[$i]['size'], $tword[$i]['angle'], $x, $tword[$i]['y'], $black, $lafont, $tword[$i]['element']);
  
           $x += $charspace;
       }
  
      // Calculate horizontal racadrage temporary cryptogram
      $xbegin = 0;
      $x = 0;
      while (($x&lt;$cryptwidth)and(!$xbegin)) {
          $y = 0;
          while (($y&lt;$cryptheight) and (!$xbegin)) {
              if (imagecolorat($imgtmp, $x, $y) != $blank)
                  $xbegin = $x;
              $y++;
          }
          $x++;
      }
  
      $xend = 0;
      $x = $cryptwidth-1;
      while (($x&gt;0) and (!$xend)) {
          $y = 0;
          while (($y&lt;$cryptheight)and(!$xend)) {
              if (imagecolorat($imgtmp, $x, $y) != $blank)
                  $xend = $x;
              $y++;
          }
          $x--;
      }
  
      $xvariation = round(($cryptwidth/2)-(($xend-$xbegin)/2));
      imagedestroy ($imgtmp);
  
      // Create the final cryptogram
      // Create the background
      $img = imagecreatetruecolor($cryptwidth, $cryptheight);
  
      if ($bgimg and is_dir($bgimg)) {
          $dh  = opendir($bgimg);
          while (false !== ($filename = readdir($dh)))
              if(eregi(&quot;.[gif|jpg|png]$&quot;, $filename))
                  $files[] = $filename;
                  closedir($dh);
                  $bgimg = $bgimg . '/' . $files[array_rand($files, 1)];
      }
  
      if ($bgimg) {
          list($getwidth, $getheight, $gettype, $getattr) = getimagesize($bgimg);
              $imgread = imagecreatefrompng($bgimg); break;
              imagecopyresized($img, $imgread, 0, 0, 0, 0, $cryptwidth, $cryptheight, $getwidth, $getheight);
              imagedestroy ($imgread);
          }
          else {
              $bg = imagecolorallocate($img, $bgR, $bgG, $bgB);
              imagefill($img, 0, 0, $bg);
              if ($bgclear)
                  imagecolortransparent($img, $bg);
          }
  
  
      function ecriture()
      {
          // CrÈation de l'Ècriture
          global  $img, $ink, $charR, $charG, $charB, $charclear, $xvariation, $charnb, $charcolorrnd, $charcolorrndlevel, $tword, $charspace;
          $ink = imagecolorallocatealpha($img, $charR, $charG, $charB, $charclear);
  
          global $question;
          global $questionLen;
  
          $x = $xvariation;
          for ($i=1; $i&lt;$questionLen; $i++) {
  
              if ($charcolorrnd) {   // Choisit des couleurs au hasard
                  $ok = false;
                  do {
                      $rndcolor = 0;
                      switch ($charcolorrndlevel) {
                          case 1 :
                              if ($rndcolor&lt;200)
                                  $ok = true; break; // tres sombre
  
                          default : $ok = true;
                      }
                  } while (!$ok);
  
                  $rndink = imagecolorallocatealpha($img, $rndR, $rndG, $rndB, $charclear);
              }
  
              $lafont = &quot;fonts/&quot; . $tword[$i]['font'];
              imagettftext($img, $tword[$i]['size'], $tword[$i]['angle'], $x, $tword[$i]['y'], $charcolorrnd ? $rndink : $ink, $lafont, $tword[$i]['element']);
  
              $x += $charspace;
          }
      }
  
  
      // Fonction permettant de dÈterminer la couleur du bruit et la forme du pinceau
      function noisecolor()
      {
          global $img, $noisecolorchar, $ink, $bg, $brushsize;
          switch ($noisecolorchar) {
              case 1  : $noisecol = $ink; break;
              case 2  : $noisecol = $bg; break;
              case 3  :
              default : $noisecol = imagecolorallocate ($img, 0, 0, 0); break;
          }
  
          if ($brushsize and $brushsize&gt;1 and function_exists('imagesetbrush')) {
              $brush = imagecreatetruecolor($brushsize, $brushsize);
              imagefill($brush, 0, 0, $noisecol);
              imagesetbrush($img, $brush);
              $noisecol = IMG_COLOR_BRUSHED;
          }
          return $noisecol;
      }
  
  
      //Adding noise: points, lines and circles random
      function bruit()
      {
          global $noisepxmin, $noisepxmax, $noiselinemin, $noiselinemax, $nbcirclemin, $nbcirclemax, $img, $cryptwidth, $cryptheight;
          $nbpx = rand($noisepxmin, $noisepxmax);
          $nbline = rand($noiselinemin, $noiselinemax);
          for ($i=1; $i&lt;$nbpx; $i++)
              imagesetpixel($img, rand(0, $cryptwidth-1), rand(0, $cryptheight-1), noisecolor());
              for ($i=1; $i&lt;=$nbline; $i++)
                  imageline($img, rand(0, $cryptwidth-1), rand(0, $cryptheight-1), rand(0, $cryptwidth-1), rand(0, $cryptheight-1), noisecolor());
      }
  
  
      if ($noiseup) {
          ecriture();
          bruit();
      }
      else {
          bruit();
          ecriture();
      }
  
  
      // Create the frame
      if ($bgframe) {
         $framecol = imagecolorallocate($img, ($bgR*3 + $charR)/4, ($bgG*3 + $charG)/4, ($bgB*3 + $charB)/4);
         imagerectangle($img, 0, 0, $cryptwidth-1, $cryptheight-1, $framecol);
      }
  
  
      // Transformations supplÈmentaires: Grayscale et Brouillage
      // VÈrifie si la fonction existe dans la version PHP installÈe
      if ($cryptgrayscal)
          imagefilter($img, IMG_FILTER_GRAYSCALE);
      if ($cryptgaussianblur)
          imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
  
      /* If I comment out these lines, then the file runs fine, but
         of course it does not return an image.
      header(&quot;Content-type: image/png&quot;); // This is the only header(...) that is
                                         // included in the file. I am requesting
                                         // the file directly with the URL.
      imagepng($img);
  
      imagedestroy ($img);
      unset ($word, $tword);
      unset ($_SESSION['cryptreload']); */
  ?&gt;
  </code></pre>
  <p>If I comment out the <code>header(&quot;content-type: image/png&quot;)</code>, it gets rid of the error, but it returns nonsense to the browser that looks like this:</p>
  <p><img src="https://i.stack.imgur.com/ymHi7.png" alt="Enter image description here" /></p>
  <p>(All this said, I am less interested in fixing this particular image file than in understanding how to go about debugging errors/unexpected behavior in image-creation files. I want to learn how to fix problematic image-generating files on my own.)</p>