【问题标题】:htaccess and image cachinghtaccess 和图像缓存
【发布时间】:2012-06-02 19:22:15
【问题描述】:

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

example.com/caching/m_images.jpg

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

example.com/recaching/m_images.jpg

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

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

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

所以函数的真正url是:

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

【问题讨论】:

    标签: .htaccess codeigniter codeigniter-url


    【解决方案1】:

    怎么样:

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

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

    然后,如果文件不存在,您可以使用file_existsreadfileheader 将文件写入浏览器。

    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-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多