通过PHP提供图像时修复.htaccess

时间:2012-03-06 09:31:45

标签: php apache .htaccess readfile php-gd

所以我试图从我的服务器重定向所有图像请求以使用自定义PHP脚本。 PHP脚本内置在一个可以通过http://www.domain.com/images/imageNameHere.jpg访问的框架中。用户显然不知道图像是否正在向他们提供。所以现在问题是这个方法目前正在为它的顶级工作,但对于后端它会导致很多问题。例如,如果我尝试在此图像上使用另一个PHP函数(如getimagesize()),则返回错误,无法打开该流。

进一步研究这个问题,我注意到这些图像的所有标题都生成了404未找到的错误。 (通过在线使用标题检测工具找到这一点,然后在error_log中看到每个加载的图像都会出现404未找到的错误)不用说我不能拥有这个,我需要所有的请求都是200的。我觉得这样做有助于解决问题。

这是我当前的.htaccess文件,我不确定如何将其设置为通过系统传递这些图像,而不是将它们视为文件而是将其视为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

以下是我当前设置PHP函数的方法(这是控制器生成的视图)

$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;
}

感谢您对此事的任何帮助:)

2 个答案:

答案 0 :(得分:0)

尝试更改此内容:

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

对此:

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

我怀疑Apache会解析重写规则,但之后只会在RewriteRule .* index.php/$0 [PT]行上对其进行过滤,因此它最终会将所有内容传递给index.php。

答案 1 :(得分:0)

HTTP请求无关紧要...因为当getimagesize拉取图像时,它与另一个浏览器相同。确保您将网址格式化为urlencode。

是否有机会获得实时网址以帮助更好地解决问题?