Speed up wordpress image file 404's - This will stop Wordpress from rendering the template and stop further process of the rest of the request as a normal page request. This will just output the error message and exit.
This will speed up wordpress if there are many missing images, or many request on the server for missing images.
Put this in your plugin or theme's function.php file.
<?php
add_action('template_redirect', function() {
if( is_404() ) {
$uri = $_SERVER["REQUEST_URI"];
if( preg_match('%.jpg|.png|.jpeg|.gif%', $uri, $matches) ) {
header('HTTP/1.0 404 Not Found');
echo "<h1>404 - Image Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();
}
}
}, 9999);
?>