PHP dynamic compression js and CSS file steps detailed

Source: Internet
Author: User
This time for you to bring PHP dynamic compression js and CSS file steps in detail, PHP dynamic compression js and CSS files Note what, the following is the actual case, together to see.

When the product is officially released, we want to compress the JS,CSS in the project to reduce the HTTP request and prevent easy viewing of the source code.

Use

Just introduce the code to the head:

<meta charset= "Utf-8"/><title>demo</title><?php//defines the files that need to be compressed $CSS = Array (css_path. ' Base.css ', Css_path. ' Style.css ', Css_path. ' My.css '); $js = Array (pub_path. '/lib/zepto.min.js ', Pub_path. '/js/1.js ', Js_path. ' 2.js ',); $css _path = Base_url (Parse_css ($CSS)); $js _path = Base_url (Parse_script ($JS));? ><link rel= "stylesheet" type= "Text/css" href= "<?php echo $css _path?>" rel= "External nofollow" >< Script src= "<?php echo $js _path?>" ></script><!--Below you can continue to put files that you cannot compress or do not want to compress--><script src= "http:/ /res.wx.qq.com/open/js/jweixin-1.0.0.js "></script>

This code generates the compressed JS and CSS in the specified directory (for example, common/). Each time you update the JS,CSS content, delete the generated compressed file first.

Example:

4e387d5edce14cc2bd6396f6ebb7921b.css7c10211f113f3175489196d336d67734.js

Attention:

Write CSS and JS files must conform to the specification. For example, be sure to add a semicolon. JS function can be without a semicolon.

References to resources

/*** Merge Compression css*/function parse_css ($urls) {$url = MD5 (implode (', ', $urls)); $path = Fcpath.  ' static/parse/'; $css _url = $path. $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 = Str_replace ("\ r \ n", "', $css _content); Clear line break $css _content = Str_replace ("\ n", "', $css _content); Clear line break $css _content = Str_replace ("\ T", "", $css _content); Clear tab $CSS _content = Str_replace (".. /images/","./...    /common/images/", $css _content);  @file_put_contents ($css _url, $css _content);  } $css _url = Str_replace (Fcpath, ", $css _url); return $css _url;}  /*** Merge Compression js*/function Parse_script ($urls) {$url = MD5 (implode (', ', $urls)); $path = Fcpath.  '/static/parse/'; $js _url = $path. $url.  '. js ';    if (!file_exists ($js _url)) {if (!file_exists ($path)) mkdir ($path, 0777);    Load_qy_lib (' Javascriptpacker '); $js _content= ''; foreach ($urls as $url) {$append _content = @file_get_contents ($url).      "\ r \ n";      $packer = new Javascriptpacker ($append _content);      $append _content = $packer->_basiccompression ($append _content);    $js _content. = $append _content;  } @file_put_contents ($js _url, $js _content);  } $js _url = Str_replace (Fcpath, ", $js _url); return $js _url;}

Project references to third-party class libraries Javascriptpacker can be downloaded from github:

Gkralik/php-uglifyjs
Https://github.com/gkralik/php-uglifyjs

/** * 9 April 2008. Version 1.1 * * This is a PHP version of the Dean Edwards JavaScript ' s Packer, * Based on: * * Parsemaster, Version 1.0 .2 (2005-08-19) Copyright 2005, Dean Edwards * a multi-pattern parser. * Known bug:erroneous behavior when using Escapechar with a replacement * value of is a function * * packer, version 2. 0.2 (2005-08-19) Copyright 2004-2005, Dean Edwards * * LICENSE:HTTP://CREATIVECOMMONS.ORG/LICENSES/LGPL/2.1/* Ported t o PHP by Nicolas Martin.  * *----------------------------------------------------------------------* changelog: * 1.1:correct a bug, ' packed ' Then unpacked becomes ' \ '. *----------------------------------------------------------------------* * Changes: * 2014-08-28:grkalik:change Class for composer support. No functionality change. * */

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP calculates an integer set of extra-large numbers

php+ajax implementation get news data case study

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.