Menu

Compress CSS Using PHP | GZIP CSS Using PHP

CSS, PHP, Tech-Byte By Aug 07, 2013 4 Comments

Compress CSS Using PHP

Hello after making the PHP Page GZIP still we need to speed up our website a bit more so we have to GZIP our CSS too so that it will load fast and help our html to load fast also. In this article we are going to compress our CSS using PHP and GZIP the CSS. Same as the PHP Page GZIP script this script is also very short and sweet. Hope you will like it. Dont forget to share it.

In this article we are using css as external css. We will write our css style and a php page where we will call it.

CSS GZIP Script

Put this script at the starting in your css.

<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>

Now save your css with .php extension. Please check again that you have changed the extension of your style file to .php from .css.

After these lines start writing your style codes.

Now just call your css in your php page same as we use to do earlier. Again we have to check for the extension we have to use .php else it will not work.

Your can check the demo here i am using the gzip css and php page gzip in my one of website here is the link  tellmequotes.com .

Without the GZIP i was having 75/100 as pagespeed from google but now i am having 91/100 check out

http://developers.google.com/speed/pagespeed/insights/?url=tellmequotes.com

Use the above code and take your website at another level.

Thank You. Like it  Use it Share it.

~~~~~ Happy Coding ~~~~~

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

4 Comments

  1. I never heard of compressing CSS with Gzip before, this could be worth an expirement :-). Thanks for the info!

  2. Uphar Srivastava says:

    Very nice tutorial with easy implementation.
    Nice work Vivek.
    I want to ask one thing that as css can we also gzip js used in a site. I have searched through net and found a result but it is bit complicated, It said to combine css and js in combine.php. Can you provide us this tutorial also? Thanks in advance.

    1. Vivek Moyal says:

      Give me the link i will try to work over that and let you know

      1. Uphar Srivastava says:

        Hi vivek,

        Thankyou for the response. here are the few links

        1.https://code.google.com/p/minify/
        2.http://www.phpdevtips.com/2013/06/magicmin-php-js-and-css-minification-class/

Leave a comment

Your email address will not be published. Required fields are marked *