Some websites have a lot of CSS files. if you merge them together and perform Gzip compression, the request and file size will be reduced, which improves the loading speed of websites. For convenience, I do not recommend manual compression and merging CSS, but use PHP code. Some websites have a lot of CSS files. if you merge them together and perform Gzip compression, the request and file size will be reduced, which improves the loading speed of websites. For convenience, I do not recommend manual compression and merging CSS, but use PHP code.
Script ec (2); script
First, put all CSS in a directory, and then create an empty CSS file named css. php In this directory (except for the suffix ).
Then put the edge code in the PHP file:
The Code is as follows: |
|
Header ('content-type: text/css '); Ob_start ("compress "); Function compress ($ buffer ){ $ Buffer = preg_replace ('! /* [^ *] ** + ([^/] [^ *] ** + )*/! ', '', $ Buffer ); $ Buffer = str_replace (array ("rn", "r", "n", "t", ''),'', $ buffer ); Return $ buffer; } /* Include('colorpicker.css '); Include('jquery-ui.css '); Include('style.css '); Include('switchery.min.css ');*/ Foreach (glob ("*. css") as $ filename) include $ filename; Ob_end_flush (); The code for introducing the CSS file is replaced with the PHP file, for example:
|
Compress multiple css into one css
The Code is as follows: |
|
/* Compress multiple CSS files into one and cache for an hour. Use the same code for Javascript, but replace below "text/css" with "text/javascript" and of course make sure you include. js files instead of. css ones. */Www.111cn.net Ob_start ("ob_gzhandler "); Header ("Content-type: text/css; charset: UTF-8 "); Header ("Expires:". gmdate ("D, d m y h: I: s", time () + 60*60). "GMT "); Include('somefile.css '); Echo "nn "; Include('anotherfile.css '); Echo "nn "; Ob_flush (); |
The following describes the JavaScript and css functions that can be compressed.
The Code is as follows: |
|
/** * Merge css styles into a file * * @ Param unknown_type $ urls * @ Param unknown_type $ path * @ Param unknown_type $ tmpl_path * @ Return unknown */ Function parse_css ($ urls, $ path = "./static/", $ tmpl_path = '.'){ $ Url = md5 (implode (',', $ urls )); $ Css_url = regular path.20.url.'.css '; If (! File_exists ($ css_url )){ If (! File_exists ($ path) mkdir ($ path, 0777 ); $ Css_content = ''; Foreach ($ urls as $ url ){ $ Css_content. = @ file_get_contents ($ url ); } $ Css_content = preg_replace ("/[rn]/", '', $ css_content ); $ Css_content = str_replace ("../images/", $ tmpl_path. "/images/", $ css_content ); @ File_put_contents ($ css_url, $ css_content ); } Return $ css_url; } /** * Merge JavaScript www.111cn.net into a file. * * @ Param unknown_type $ urls * @ Param unknown_type $ path * @ Return unknown */ Function parse_script ($ urls, $ path = "./static /"){ $ Url = md5 (implode (',', $ urls )); $ Js_url = $ path. $ url. '. js '; If (! File_exists ($ js_url )) { If (! File_exists ($ path) mkdir ($ path, 0777 ); Require_once "inc/javascriptpacker. php "; $ Js_content = ''; Foreach ($ urls as $ url) { $ Append_content = @ file_get_contents ($ url). "rn "; $ Packer = new JavaScriptPacker ($ append_content ); $ Append_content = $ packer-> pack (); $ Js_content. = $ append_content; } @ File_put_contents ($ js_url, $ js_content ); } Return $ js_url; } Front-end Js call $ Pagejs [] = $ tplurl. "js/jump. js "; $ Jsfile = parse_script ($ pagejs, "./template/default/js /","."); ?>
|