Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2. /**
  3.   * Passes through image from cache folder to render.
  4.   * @param string $fileName
  5.   * @param string $fileExtension
  6.   */
  7.  public static function renderImageFromCacheFolder($fileName, $fileExtension){
  8.      $filePath = self::getImagesCacheFolderPath(). $fileName . ".". $fileExtension;
  9.      // get the last modification of the image
  10.      $lastModified = filemtime($filePath);
  11.  
  12.     //Modify last access time by using touch, so when we run cleanCache we can delete all images not accessed in the last 30 days
  13.     // ensure only access time is changed, not modification time, do this by setting new modification time to the
  14.     // previous one, and access time to current time()
  15.     touch($filePath, date('U', $lastModified), time());
  16.    
  17.     // get a unique hash of this image
  18.     // this is called an etag
  19.     $etagFile = md5_file($filePath);
  20.    
  21.     // get the HTTP_IF_MODIFIED_SINCE header if set
  22.     // imagine this as the browser saying to your server:
  23.     //   "Don't mind sending me the image if it wasn't modified since this date, I already have it then"
  24.     $ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
  25.    
  26.     // get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
  27.     // imagine this as the browser saying to your server:
  28.     //  "Don't mind sending me the image if it has this etag, if file's etag on the server is the same
  29.     //      etag that I have, then we have the same image, Don't send it to me, I already have it"
  30.     $etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
  31.  
  32.     //set last-modified header
  33.     header("Last-Modified: ".gmdate("r", $lastModified));
  34.     //set etag-header
  35.     header("Etag: $etagFile");
  36.     //make sure caching is turned on
  37.     header('Cache-Control: public');
  38.  
  39.     //check if page has changed. If not, send 304 and exit
  40.     // if file wasn't modified, or etag still the same
  41.     // then we don't need to send it again
  42.     // we check using "or" because here we use md5_file to get the etag. then *even* if the modified date changes
  43.     // if md5_file stilll returns the same result, the file didn't change at all
  44.     if (($ifModifiedSince && @strtotime($ifModifiedSince)==$lastModified) || $etagHeader == $etagFile)
  45.     {
  46.            header("HTTP/1.1 304 Not Modified");
  47.            exit;
  48.     }
  49.  
  50.     // at this point we need to send the image to the browser because it's not in browser cache
  51.     header("Content-Type: image/$fileExtension");
  52.     header('Content-Length: ' . filesize($filePath));
  53.  
  54.     echo file_get_contents($filePath);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement