PHP tips: How to Use JS and CSS optimization tool Minify

Source: Internet
Author: User

I. Implement code for merging and compressing multiple JS and CSS files

HTML:

Copy codeThe Code is as follows:
<Link rel = "stylesheet" type = "text/css" href = "cssmin. php? Get = base, style1, style2, global & path = css/& amp; v = 20131023 "/>
<Script type = "text/javascript" src = "jsmin. php? Get = jquery-1.6.4.min.js, minjquery. js, minjquery. ui. js, test. js, global. js & path = js/& v = 20131023 "> </script>

PHP:

Copy codeThe Code is as follows:
// Output JS
Header ("Content-type: Application/x-javascript; Charset: UTF-8 ");
If (isset ($ _ GET )){
$ Files = explode (",", $ _ GET ['get']);
$ Str = '';
Foreach ($ files as $ key => $ val ){
$ Str. = file_get_contents ($ _ GET ['path']. $ val );
}

$ Str = str_replace ("\ t", "", $ str); // clear spaces
$ Str = str_replace ("\ r \ n", "", $ str );
$ Str = str_replace ("\ n", "", $ str );

// Delete a single row comment
$ Str = preg_replace ("// \/\ s * [a-zA-Z0-9 _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] */", "", $ str );
// Delete comments from multiple rows
$ Str = preg_replace ("// \/\ * [^ \/] * \/s", "", $ str );

Echo $ str;
}

// Output CSS
Header ("content-type: text/css; charset: UTF-8 ");
If (isset ($ _ GET )){
$ Files = explode (",", $ _ GET ['get']);
$ Fc = '';
Foreach ($ files as $ key => $ val ){
$ Fc. = file_get_contents ($ _ GET ['path']. $ val. ". css ");
}
$ Fc = str_replace ("\ t", "", $ fc); // clear spaces
$ Fc = str_replace ("\ r \ n", "", $ fc );
$ Fc = str_replace ("\ n", "", $ fc );
$ Fc = preg_replace ("// \/\ * [^ \/] * \/s", "", $ fc );
Echo $ fc;
}

It is just a simple prototype and is not encapsulated. In addition, remember to cache the merged files.

Ii. Usage of Minify

1. download the latest version of minifyfrom code.google.com/p/minify/and decompress it. Copy the "min" folder along with the content to the DOCUMENT_ROOT directory (website and directory ).

You can modify the folder name "min"

2. Configure g parameters in "min/groupsConfig. php ".

Copy codeThe Code is as follows:
Return array (
// 'Js' => array ('// js/file1.js',' // js/file2.js '),
// 'Css '=> array (' // css/file1.css ',' // css/file2.css '),
);

3. You can reference the web page as follows:

<Script type = "text/javascript" src = "/min/g = js & 20140519"> </script>

The subsequent numbers can be marked by the updated date. "min" corresponds to the name in step 1.

4. For performance optimization, see code.google.com/p/minify/wiki/CookBook.

Note:

1. Enable the rewrite_module module in httpd. conf.

2. During the development process, you can enable the debugging mode and disable the debugging mode after development. You can view the firebug in Firefox browser.

Copy codeThe Code is as follows:
$ Min_allowDebugFlag = true

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.