title: Convert animated GIF to PNG
body: <p>I'm trying to display only the first frame of an animated GIF to a PNG to use as a thumbnail. I currently have this code:</p>
  
  <pre><code>&lt;?php
  function myImageCreateFromGif($file_or_url) { 
  $dummy_file = "/tmp/dummy.gif"; 
  # if this is a url, use fopen to get the file data, then 
  # save it to a dummy file 
  if (preg_match("/(http|ftp):\/\//i", $file_or_url)) { 
          # open the file using fopen, which supports remote URLs 
          $input = fopen($file_or_url, "rb"); 
          # read the contents of the file 
          # will accept files up to 10Mb, but will probably get 
          # and EOF before that, we have to do it this way because 
          # filesize isn't designed to work with URLs.  sigh. 
          $image_data = fread($input, 10000000); 
          fclose($input); 
          # write the contents to a dummy file 
          $output = fopen("$dummy_file", "wb"); 
          fwrite($output, $image_data); 
          fclose($output); 
          # create the gif from the dummy file 
          $image = ImageCreateFromGif($dummy_file); 
          # get rid of the dummy file 
          unlink($dummy_file); 
      } 
      # if it's not a URL, we can simply open the image directly 
      else { 
          $image = ImageCreateFromGif($file_or_url); 
      } 
      if ($image) { return $image; } 
      else { return 0; } 
  }
  $image = "http://i.imgur.com/".$_GET["i"].".gif";
  $img = myImageCreateFromGif($image);
  if($img) {
      header("Content-Type: image/png");
      ImagePNG($img);
      ImageDestroy($img);
  }
  ?&gt;
  </code></pre>
  
  <p>Which works fine, but the GIF does not fully load before it becomes a PNG, so the page returns either a broken image if the GIF could not load at all, or a partially-loaded GIF that fills the unloaded part by repeating what it has managed to load. So, how can I get the GIF to load completely before making it a PNG?</p>