title: Fastest way to redirect missing image files
body: <p>I have an on-the-fly thumbnailing system and am trying to find the best way to make sure it's as fast as possible when serving up images. Here is the current flow:</p>
  
  <ul>
  <li>User requests thumbnail <code>thumbnails/this-is-the-image-name.gif?w=200&amp;h=100&amp;c=true</code></li>
  <li>htaccess file uses modrewrite to send requests from this folder to a PHP file</li>
  <li>PHP file checks <code>file_exists()</code> for the requested image based on the query string values</li>
  </ul>
  
  <p>If it does:</p>
  
  <pre><code>header('content-type: image/jpeg'); 
  echo file_get_contents($file_check_path); 
  die();
  </code></pre>
  
  <p>If it doesn't it creates the thumbnail and returns it.</p>
  
  <p>My question is whether there is a way to optimize this into being faster? Ideally my htaccess file would do a file_exists and only send you to the PHP file when it doesn't... but since I am using query strings there is <a href="https://stackoverflow.com/questions/6302529/htaccess-cant-check-dynamic-file-existence/">no way to build a dynamic URL to check</a>. Is it worth switching from query strings to an actual file request and then doing the existence check in htaccess? Will that be any faster? I prefer the query string syntax, but currently all requests go to the PHP file which returns images whether they exist or not.</p>
  
  <p>Thank you for any input in advance!</p>