2

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?

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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).

Comments

1

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)

2 Comments

Don’t forget the character encoding declaration.
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.
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.