tags:

views:

420

answers:

6

Now I'm not making use of any compression method,just put the below between the body tag:

<link rel="stylesheet" type="text/css" href="Client/Css/all.css" />

Is it possible to use ob_start("ob_gzhandler") in PHP to compress this css file?

And how?

+4  A: 

this is not generally done programmatically, it is handled by the web server

For apache, there's a mod_gzip or mod_deflate module that you can use.

IIS has settings that you can use.

Joel Martinez
Unless he's using an older version, he's going to want to use mod_deflate, not mod_gzip.
jacobangel
I don't understand :gzip is used more than deflate,why Mod_Gzip is older than mod_deflate?
Shore
+2  A: 

Yes you could (when piping the files through a php script that performs the compression). But there is much more to it than simply compressing the output. You should only send compressed output to clients that support it, you should make sure you properly handle caching information/requests. Etc.

In quite some cases your webserver contains support for doing all that for you without the need of a PHP script. In case of Apache look at mod_deflate and mod_mime (directive AddEncoding).

elmuerte
Also, mod_expires
Piskvor
A: 

There are several ways.

You could tell the webserver to treat the file as PHP (renaming to have a .php extension would be the simplest) and then add:

<?php
  header("Content-type: text/css; charset=utf-8");
  ob_start("ob_gzhandler")
?>

to the top.

You could write a PHP script that does the same thing, but reads in the CSS file instead of having it inline.

Both this options lead to caching issues - you would have to take care of the cache control HTTP headers too if you want to be sane about it.

The best option would be to forget about PHP and just configure your webserver to compress those files (using mod_deflate, mod_gzip or similar)

David Dorward
Don’t forget the character encoding declaration.
Gumbo
It's fairly irrelevent - only very very rare edge cases don't use plain ASCII - which is a subset of any character encoding likely to be used.
David Dorward
A: 

What are you trying to accomplish? If you're trying to shrink components to send to the clients browser so that they're downloaded more quickly, you should be using mod_defalte. If that's not an option, you can find more here: first result from googling "gzip php css"

EDIT: I just want to point out that unless you're using an older version of Apache, you should use mod_deflate not mod_gzip.

jacobangel
A: 

When a user visits your site, PHP renders the markup but the client downloads .js and .css from the web server, independently of PHP. Therefore, you cannot use PHP to compress the css on the fly.

Instead use Apache mod_deflate. In addition, you can use an external css compressor such as YUI Compressor to further reduce the size of the css file.

jonstjohn
+1  A: 

If you are using a newer version of the apache web server add the following to your .htaccess file:


DeflateCompressionLevel 9  
SetOutputFilter DEFLATE  
BrowserMatch ^Mozilla/4 gzip-only-text/html  
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html  
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|ico)$ no-gzip dont-vary  
Header append Vary User-Agent env=!dont-vary


Rick Lavigne