2012-05-28 38 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