2013-03-28 1 views
0

글쎄, 제목에서 알 수 있듯이, 사용자의 브라우저에서 PHP 스크립트로 생성 된 리소스를 캐싱하고 싶습니다. 나는 서버 측에서 일부 이미지에 대한 축소판 이미지를 동적으로 제공하기 때문에 그렇게하고 싶다. 나는이 내용이이 .htaccess 파일을 가지고 있습니다.캐싱 PHP 스크립트로 생성 된 이미지

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule ^(.+)$ index.php?image_id=$1 [QSA,L] 

index.php 파일에 다음 코드가 포함되어 있습니다. 비록 내가 개발자 모드로 이동 구글 크롬의 네트워크 탭을 볼 때이 후

<?php 

     header('Last-Modified: ' .gmdate('D, d M Y H:i:s',time()-3600) . ' GMT'); 


$image_id=$_GET['image_id']; 
$chunk= explode("/", $image_id); 
$destination_height=$chunk[0]; 
$destination_width=$chunk[1]; 
$image=$chunk[2]; 
if(file_exists($image)) 
{ 
$extension=explode(".",$image); 
$extension=end($extension); 
switch ($extension) { 
    case "jpg": 
     $source_image= imagecreatefromjpeg($image); 
     break; 
case "jpeg": 
     $source_image= imagecreatefromjpeg($image); 
     break; 
    case "gif": 
     $source_image= imagecreatefromgif($image); 
     break; 
    case "png": 
     $source_image= imagecreatefrompng($image); 
     break;  
    default: 
     break; 
} 
     $source_height = imagesy($source_image); 
     $source_width = imagesx($source_image); 
     if($destination_height=="full") 
     { 
      $destination_height=$source_height; 
     } 
     if($destination_width=="full") 
     { 
      $destination_width=$source_width; 
     } 
     $destination_image= imagecreatetruecolor($destination_width, $destination_height); 
    $dst_x=0; 
    $dst_y=0; 
    $src_x=0; 
    $src_y=0; 
    imagecopyresized($destination_image,$source_image,$dst_x,$dst_y,$src_x,$src_y,$destination_width,$destination_height,$source_width,$source_height); 
switch ($extension) { 
    case "jpg": 
    imagejpeg($destination_image); 
    header("content-type:image/jpeg"); 
     break; 
    case "jpeg": 
    header("content-type:image/jpeg"); 
    imagejpeg($destination_image); 
     break; 
    case "gif": 
    header("content-type:image/gif"); 
    imagegif($destination_image); 
     break; 
    case "png": 
    header("content-type:image/png"); 
    imagepng($destination_image); 
     break; 
    default: 
    break; 
} 
} 
?> 

, 상태 정보는 다음과 같습니다 200 OK 스크립트에 의해 생성 된 모든 이미지를 캐시 할 수있는 일
? 이 구현에서는 특정 URL에 대해 내용이 변경되지 않으므로 이미지를 캐시하려고합니다. 어떤 종류의 아이디어라도 감사 할 것입니다. 감사.

답변

0

그것은 서버 및 이후 이미지를 작성합니다이

file_put_contents($filename, $imagedata); 

만 사용 캐시를 제공 한 다음 파일이 한 번만 파일을 작성합니다 존재하지 않는 경우 이미지 생성을 실행됩니다 htaccess로 파일 그때부터 버전.

+0

자동 삭제는 어떻게됩니까? 실제로 나는 내 서버에 불필요한 파일을 가지고있는 파일의 거대한 클러스터를 원하지 않습니다. 나는 실시간으로 생성되기를 원합니다. –

+0

캐시 된 ** 및 ** 실시간으로 생성 될 수 없습니다. 그것은 하나 또는 다른 것입니다. – chrislondon

+0

믿을 수 없다면 실시간으로 캐시 할 수 있습니다. 그런 다음 위의 헤더 행을 제거하고 유지하십시오. 마침내 준비가 완료되었습니다. 'header ("Last-Modified :".date ("D, d M Y H : i : s", 시간() - 360000)); 헤더 ('만료 :'.gmdate ('D, d M Y H : i : s \ G \ M \ T', 시간() + 3600000)); //// 브라우저가 캐시 된 복사본이있는 경우 $ _SERVER [ 'HTTP_IF_MODIFIED_SINCE' 304 헤더를 보내십시오 ('Last-Modified :'. $ _ SERVER [ 'HTTP_IF_MODIFIED_SINCE'], true, 304); \t 종료; }' –

관련 문제