This code does not do what the comment says:
// Locate the position of the last hyphen in the filename.
$index = strpos( $filename, "-" );
You expend a lot of effort to parseConsider parsing the path — a task that can be accomplished more easily and clearly using a regular expression match.
For security, I've explicitly listed the allowed characters for the image filename.
# You probably want to analyze PATH_INFO rather than QUERY_STRING
$path = pathinfo($_SERVER['PATH_INFO']);
if (!preg_match('!^([A-Za-z0-9_]+)-(\d+)?x(\d+)?$!', $path['filename'], $matches)) {
header('HTTP/1.0 404 Not Found');
return;
}
$orig_image = sprintf('%s%s/%s.%s',
$IMAGES_DIRECTORY, $path['dirname'],
$matches[1], $path['extension']);
$width = $matches[2];
$height = $matches[3];
if (!file_exists($orig_image)) {
header('HTTP/1.0 404 Not Found');
return;
}
# That's it for the URL parsing code! Let's get on with the real work!
serve_image($orig_image, $width, $height);