Search code examples
phpapache.htaccessreadfilephp-gd

Fixing .htaccess when serving images via PHP


So I am trying to redirect all image requests from my server to use a custom PHP script. The PHP script is built into a framework which can be accessed through http://www.domain.com/images/imageNameHere.jpg The user obviously doesn't know that the image is being served to them. So now the problem is that this method is currently working for the top level of it, but for the back-end it's causing a lot of issues. For instance if I try to use another PHP function such as getimagesize() on this image I am returned with an error that it can't open the stream.

Looking further into the matter I noticed that all of the headers for these images are generating 404 not found errors. (Found this out by using header detection tools online and then seeing in the error_log that every image loaded comes up with a 404 not found error) Needless to say I can't have this, and I need all the requests to be 200's. I feel like doing this will help to fix the issue.

This is my current .htaccess file, I am not exactly sure how to set it up to pass these images through the system and not treat them as a file but rather as a URI:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

RewriteCond %{HTTP_HOST} ^i.tinyuploadz.com
RewriteRule (.*) http://www.tinyuploadz.com/images%{REQUEST_URI} [NC,L,NS]

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Image rediects?
#RewriteRule /images/(.+)\.jpg /images/$1.jpg [PT]

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

# Fix the CSS an d JS
#RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]

# Do not let people browse our directories
Options -Indexes

Here is how I have my PHP function set up currently (this is the view generated by the controller)

$day = date("d", $image['upload_time']);
$month = date("m", $image['upload_time']);
$year = date("Y", $image['upload_time']);
$url = IMAGE_URL .  $year . '/' . $month . '/' . $day . '/' . $image['image_id'] . '.' . $image['extension'];

header('Content-type: ' . $image['mime']);
switch ($image['extension'])
{
    case 'png':
        $img = @imagecreatefrompng($url);
        imagepng($img);
        imagedestroy($img);
        break;
    case 'jpg':
    case 'jpeg':
        $img = @imagecreatefromjpeg($url);
        imagejpeg($img);
        imagedestroy($img);
        break;
    case 'gif':
        if (is_ani($url))
        {
            readfile($url);
            break;
        }
        $img = @imagecreatefromgif($url);
        imagegif($img);
        imagedestroy($img);
        break;
    case 'bmp':
        // Seems like we can't do anything special for bmps
        readfile($url);
        break;
    default:
        readfile($url);
        break;
}
//readfile($url . $image['image_id'] . '.' . $image['extension']);

// We don't want Kohana to break anything else with the header change
exit();

function is_ani($filename) 
{
    if(!($fh = @fopen($filename, 'rb')))
        return false;
    $count = 0;
    //an animated gif contains multiple "frames", with each frame having a 
    //header made up of:
    // * a static 4-byte sequence (\x00\x21\xF9\x04)
    // * 4 variable bytes
    // * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)

    // We read through the file til we reach the end of the file, or we've found 
    // at least 2 frame headers
    while(!feof($fh) && $count < 2) 
    {
        $chunk = fread($fh, 1024 * 100); //read 100kb at a time
        $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
    }

    fclose($fh);
    return $count > 1;
}

Thanks for any help on the matter :)


Solution

  • Try changing this:

    #RewriteRule /images/(.+)\.jpg /images/$1.jpg [PT]
    

    To this:

    RewriteRule /images/(.+)\.jpg /images/$1.jpg [PT,L]
    

    What I suspect is that the Apache parses the rewrite rule, but then only filters it back on the RewriteRule .* index.php/$0 [PT] line, so it's really passing everything to index.php in the end.