0

需要帮助,我想创建一个图像访问缓存,url 处的示例图像

example.com/caching/m_images.jpg

如果该图像存在,则显示图像,但如果不存在,则可能使用 .htaccess 脚本在以下位置运行图像功能:

example.com/recaching/m_images.jpg

在缓存文件夹中创建具有自定义大小的新图像,然后再次访问图像 url。如何做到这一点,或者有另一个更好的解决方案?谢谢。

ps 我已经有很少的脚本 htaccess 来删除 'index.php?' 来自 codeigniter 网址

重写引擎开启
RewriteCond $1 !^(index\.php|js|css|缓存)
重写规则 ^(.*)$ index.php/$1 [L]

所以函数的真正网址是:

example.com/index.php?recaching/m_images.jpg

4

1 回答 1

1

怎么样:

RewriteEngine on
RewriteRule ^images/(.*)$ index.php/recaching/$1 [L,QSA]
RewriteCond $1 !^(index\.php|js|css|caching)
RewriteRule ^(.*)$ index.php/$1 [L]

上面的“应该”(我没有测试过)重定向图像文件夹中的任何请求的文件(假设你有一个图像文件夹)并将路径传递给重新缓存的路径。

file_exists然后,您可以使用readfile并将header文件写入浏览器(如果它不存在)。

IE

function recaching($path)
{
    $tmpPath = FCPATH . 'images/' . $path;
    if(!file_exists($tmpPath))
    {
        // change $tmpPath to a file that exists, i.e. a 404 image
        // or if you are generating an image when one doesn't exist
        // create it here.
    }
    $info = getimagesize($tmpPath);
    if($info !== false)
    {
        header('content-type: ' . $info['mime']);
        readfile($tmpPath);
    }
    else
    {
        die('There was a problem rendering the image.');
    }
}
于 2012-05-28T14:02:18.303 回答