Note: After the compressed files are cached on the server, the re-access will reduce the re-compression time and reduce the CPU usage. By setting the client file cache time, you can reduce the number of re-requests by more than 85%. Because the image is already in the compression format, it only sets the client cache time and does not compress the image.Usage: The server must support the gzip and Rewrite functions. Add the following code RewriteRule (.. Css $ |.. Js $ |.. Jpg $ |.. Gif $ |. *. png $) gzip. php? $1 Upload gzip. php to the root directory4. create a cache folder in the root directory to ensure read/write.
- /**
- * @ Author Seraphim
- * @ Copyright 2012
- */
- //
- Function sendheader ($ last_modified, $ p_type, $ content_length = 0)
- {
- // Set the client cache validity period
- Header ("Expires:". gmdate ("D, d m y h: I: s", time () + 15360000). "GMT ");
- Header ("Cache-Control: max-age = 315360000 ");
- Header ("Pragma :");
- // Set the last modification time
- Header ("Last-Modified:". $ last_modified );
- // Set file type information
- Header ($ p_type );
- Header ("Content-Length:". $ content_length );
- }
- Define ('abspath', dirname (_ file __).'/');
- $ Cache = true;
- $ Cachedir = 'cache/'; // Directory for storing the gz file to ensure that the file can be written
- If (empty ($ _ SERVER ['query _ string'])
- Exit ();
- $ Gzip = strstr ($ _ SERVER ['http _ ACCEPT_ENCODING '], 'gzip ');
- If (empty ($ gzip ))
- $ Cache = false;
- $ Key = array_shift (explode ('? ', $ _ SERVER ['query _ string']);
- $ Key = str_replace ('../', '', $ key );
- $ Filename = ABSPATH. $ key;
- $ Symbol = '_';
- $ Rel_path = str_replace (ABSPATH, '', dirname ($ filename ));
- $ Namespace = str_replace ('/', $ symbol, $ rel_path );
- $ Cache_filename = ABSPATH. $ cachedir. $ namespace. $ symbol. basename ($ filename ).
- '.Gz '; // Generate the gz file path
- $ Ext = array_pop (explode ('.', $ filename); // you can specify the file type based on the suffix.
- $ Type = "Content-type: text/html"; // Default file type
- Switch ($ ext)
- {
- Case 'css ':
- $ Type = "Content-type: text/css ";
- Break;
- Case 'js ':
- $ Type = "Content-type: text/javascript ";
- Break;
- Case 'GIF ':
- $ Cache = false;
- $ Type = "Content-type: image/gif ";
- Break;
- Case 'jpg ':
- $ Cache = false;
- $ Type = "Content-type: image/jpeg ";
- Break;
- Case 'PNG ':
- $ Cache = false;
- $ Type = "Content-type: image/png ";
- Break;
- Default:
- Exit ();
- }
- If ($ cache)
- {
- If (file_exists ($ cache_filename ))
- {// If the gz file exists
- $ Mtime = filemtime ($ cache_filename );
- $ Gmt_mtime = gmdate ('d, d m y h: I: S', $ mtime). 'gmt ';
- If (isset ($ _ SERVER ['http _ IF_MODIFIED_SINCE ']) & array_shift (explode ('; ', $ _ SERVER ['http _ IF_MODIFIED_SINCE']) =
- $ Gmt_mtime ))
- {
- // The file modification date is the same as that in the browser cache, and 304 is returned.
- Header ("HTTP/1.1 304 Not Modified ");
- // Send the client header
- Header ("Content-Encoding: gzip ");
- Sendheader ($ gmt_mtime, $ type );
- }
- Else
- {
- // Read the gz file output
- $ Content = file_get_contents ($ cache_filename );
- // Send the client header
- Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
- Header ("Content-Encoding: gzip ");
- // Send data
- Echo $ content;
- }
- }
- Else
- If (file_exists ($ filename ))
- {// No corresponding gz file
- $ Mtime = mktime ();
- $ Gmt_mtime = gmdate ('d, d m y h: I: S', $ mtime). 'gmt ';
- // Read the file
- $ Content = file_get_contents ($ filename );
- // Remove the blank part
- // $ Content = ltrim ($ content );
- // Compress the file content
- $ Content = gzencode ($ content, 9, $ gzip? FORCE_GZIP: FORCE_DEFLATE );
- // Send the client header
- Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
- Header ("Content-Encoding: gzip ");
- // Send data
- Echo $ content;
- // Write a file
- File_put_contents ($ cache_filename, $ content );
- }
- Else
- {
- Header ("HTTP/1.0 404 Not Found ");
- }
- }
- Else
- {// The output in Gzip mode is not used for processing. The principle is basically the same as above.
- If (file_exists ($ filename ))
- {
- $ Mtime = filemtime ($ filename );
- $ Gmt_mtime = gmdate ('d, d m y h: I: S', $ mtime). 'gmt ';
- If (isset ($ _ SERVER ['http _ IF_MODIFIED_SINCE ']) & array_shift (explode ('; ', $ _ SERVER ['http _ IF_MODIFIED_SINCE']) =
- $ Gmt_mtime ))
- {
- // The file modification date is the same as that in the browser cache, and 304 is returned.
- Header ("HTTP/1.1 304 Not Modified ");
- // Send the client header
- Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
- Header ("Content-Encoding: gzip ");
- }
- Else
- {
- // Read file output
- $ Content = file_get_contents ($ filename );
- // Send the client header
- Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
- // Send data
- Echo $ content;
- }
- }
- Else
- {
- Header ("HTTP/1.0 404 Not Found ");
- }
- }
- ?>
|