2012-05-28 97 views
0

需要帮助,我想创建一个图像访问缓存,比如图像的URLhtaccess以及图像缓存

example.com/caching/m_images.jpg

如果然后该图像显示存在图像,但如果不存在,可能与的.htaccess脚本,运行图像功能为:

example.com/recaching/m_images.jpg

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

ps 我已经有小脚本htaccess删除'index.php?'从笨网址

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

这样的功能,真正的网址是:

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

回答

1

如何:

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

上面的“应该”(我还没有测试过)重定向图像文件夹内的任何请求的文件(assum你有一个图像文件夹)并将路径传递给路线回收。

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

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.'); 
    } 
} 
+0

谢谢,对不起下旬进行更新。 – timenz