SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Florida
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JS and CSS Compression with PHP

    Is there any way to configure PHP to compress not only PHP output but Javascripts files (.js) and CSS files (.css) that are referred into the PHP script?

    I have enabled "zlib.output_compression on" and "zlib.output_compression_level 3", but I afraid that doesn't affect to JS and CSS file formats.

    Thank you very much in advance.

    Mapg

  2. #2
    SitePoint Member FreMEliCa's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here:
    http://fiftyfoureleven.com/sandbox/w...s-gzip-method/

    Similar process for JS I think.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Florida
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have added AddType application/x-httpd-php .php .css .js

    and works ... At least I see these files (.css and .js) as gzipped at ...

    http://leknor.com/code/gziped.php

    So I am not sure if it's necessary to add some kind of header to css or js files like as follow ...

    PHP Code:
    <?php
    ob_start
    (); 
    header("Content-Type: text/css");
    ?>
    ... or an prepend_file tag in .htaccess where styles are placed, namely ...

    php_value auto_prepend_file gzip-css.php (as noted in the link that you provided)

    Is it enough if I saw gzipped my css files at http://leknor.com/code/gziped.php?

    Thanks

    Mapg

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    congratulation for the site and the script!
    really impressive!
    nice job

  5. #5
    SitePoint Member FreMEliCa's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From:
    http://www.fiftyfoureleven.com/sandb...s-gzip-method/

    1. You save this snippet as gzip-css.php:
    Code:
    <?php 
    ob_start ("ob_gzhandler");
    header("Content-type: text/css");
    header("Cache-Control: must-revalidate");
    $offset = 60 * 60 ;
    $ExpStr = "Expires: " . 
    gmdate("D, d M Y H:i:s",
    time() + $offset) . " GMT";
    header($ExpStr);
    ?>
    2. Add this snippet to your htaccess:
    Code:
    AddHandler application/x-httpd-php .css
    php_value auto_prepend_file gzip-css.php
    Make sure that if you do this that your CSS files are in a directory all by themselves.

    Since you probably don't want to do that, and you want your JS gzipped you should do the following:

    Keep your htaccess rule of:
    Code:
    AddType application/x-httpd-php .php .css .js
    Then put this at the start of your CSS files:
    Code:
    <?php 
    ob_start ("ob_gzhandler");
    header("Content-type: text/css");
    header("Cache-Control: must-revalidate");
    $offset = 60 * 60 ;
    $ExpStr = "Expires: " . 
    gmdate("D, d M Y H:i:s",
    time() + $offset) . " GMT";
    header($ExpStr);
    ?>
    and this at the start of your JS files:
    Code:
    <?php 
    ob_start ("ob_gzhandler");
    header("Content-type: text/javascript");
    header("Cache-Control: must-revalidate");
    $offset = 60 * 60 ;
    $ExpStr = "Expires: " . 
    gmdate("D, d M Y H:i:s",
    time() + $offset) . " GMT";
    header($ExpStr);
    ?>
    Remember that we need to make sure that our CSS files, though being parsed by PHP need to be sent with a MimeType of text/css or it won't display in stricter browsers, and similarly for JS.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Florida
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thank you very much for your reply.

    Seems that configuring PHP with ...

    Code:
    zlib.output_compression on
    or with ...

    Code:
    php_flag zlib.output_compression on
    in the main .htaccess (root directory)

    ... it's not necessary to add "ob_gzhandler" in every css or js file.

    After that,

    Code:
    AddType application/x-httpd-php .php .css .js
    ... is a must, of course.

    I am agree with you that ...

    Code:
    header("Content-type: text/css");
    and ...

    Code:
    header("Content-type: text/javascript");
    ... is a good option to avoid problems with strict browsers

    But ...
    Code:
    * snip * 
    header("Cache-Control: must-revalidate");
    $offset = 60 * 60 ;
    $ExpStr = "Expires: " . 
    gmdate("D, d M Y H:i:s",
    time() + $offset) . " GMT";
    header($ExpStr);
    * snip *
    ... could be handled by the html code itself with common tags.

    I also added ...

    Code:
    zlib.output_compression_level 3
    because works fine and CPU is not overloaded.

    ... or if you prefer a htaccess file ...

    Code:
    php_value zlib.output_compression_level 3
    I suggest do several tests at ...
    http://leknor.com/code/gziped.php

    Best Regards,

    Mapg


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •