3 ways to compress CSS files using PHP
When you’re using a sophisticated design, CSS files can quickly become very long, and takes time to load. I have compiled 3 interresting ways of compressing CSS files by using PHP.
The Paul Stamatiou method
This method is the first I learnt, one year ago or so. In order to achieve it, you first have to rename your .css file to .css.php.
Make sure to import it in your html file by using their new name:
<link rel="stylesheet" type="text/css" media="screen" href="/style.css.php"/>
Once you successfully rename your css files, edit it and add the following code at the beginning of the file:
<?php if(extension_loaded('zlib')){ob_start('ob_gzhandler');} header("Content-type: text/css"); ?>Then, add the next line to the very bottom and save the file.
<?php if(extension_loaded('zlib')){ob_end_flush();}?>That’s all. While this method is useful and efficient.
→ Source
The Perishable Press method
Basically, The Perishable Press method works as Paul Stamatiou’s method, by renaming your .css files to .css.php (or .php alone) and adding this short code snippet on the beggining of your CSS file:
<?php
ob_start ("ob_gzhandler");
header ("content-type: text/css; charset: UTF-8");
header ("cache-control: must-revalidate");
$offset = 60 * 60;
$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
header ($expire);
?>I prfer this method this method to the one described by Paul Stamatiou because you don’t have to edit both the beginning and the end of the css file.
→ Source
The Reinhold Weber method
I just stumbled upon this code snippet by German developer Reinhold Weber some minutes ago. The least I can say is that I like it.
<?php
header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
/* remove comments */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
return $buffer;
}
/* your css files */
include('master.css');
include('typography.css');
include('grid.css');
include('print.css');
include('handheld.css');
ob_end_flush();
?>
Why I like it? Because it is the only one of the 3 methods above which doesn’t require you to rename the CSS files to .php. Very nice to use on an existing site. The regular expression to strip out css comments is very nice too.
→ Source
You probably guessed it, my favorite method of the 3 above is the last one. And you? Did you already tried any of theses methods? Which one is your favorite? Tell us in the comments.
Pingback: PHP ile CSS Dosyalarınızı Sıkıştırmak için 3 Farklı Yöntem | Barış Yüksel | brsyuksel()
Pingback: Free Premium Social Media WordPress Theme: WicketPixie » Wordpress themes marketplace()
Pingback: Wordpress Blog Services - 2008 Most Popular Design posts, Tutorials and Resources()
Pingback: 2008年最佳Webè®¾è®¡æ–‡ç« ã€æ•™ç¨‹åŠèµ„æº - 刀客å¾é€”()
Pingback: 3 ways to compress CSS files using PHP | CNSQ Online Blog()
Pingback: 3 ways to compress CSS files using PHP | Urban Mainframe()
Pingback: 2008 Most Popular Design posts, Tutorials and Resources | Web Hosting and Domains()
Pingback: 2008 Most Popular Design posts, Tutorials and Resources | SulVision()
Pingback: Have to say » Blog Archive » 50个Webå¼€å‘è®¾è®¡æ–‡ç« ã€æ•™ç¨‹åŠèµ„æº()
Pingback: tutorialand.com()
Pingback: *real me » Bookmarks on 2009-01-19()
Pingback: 2008 Most Popular Design posts, Tutorials and Resources | Web2.0()
Pingback: 20 Useful PHP Components & Tutorials for Everyday Project | Noupe()
Pingback: Wordpress Blog Services - 20 Useful PHP Components & Tutorials for Everyday Project()
Pingback: blog.no-panic.at » links for 2009-03-03()
Pingback: php note» Blog Archive » CSS圧縮転é€ã«ã¤ã„ã¦()
Pingback: 20 Useful PHP Components & Tutorials for Everyday Project « Hdeya team blog()
Pingback: Angels Picks » Blog Archive » 20 Useful PHP Components & Tutorials for Everyday Project()
Pingback: Comprimir CSS con PHP « Gambitomeister()
Pingback: Blog InterGraphicDESIGNS » GuÃa para comprimir archivos CSS con PHP?()
Pingback: How To Achive Maximum Website Performance (Part 3) « Sascha Kimmel - Living The Web Experience Since 1996()
Pingback: links for 2009-05-25 | NeXt()
Pingback: WebDev Best Practices – Parte 1 | José Ricardo()
Pingback: Waschen, schneiden, legen - oder wie man CSS Dateien gekonnt optimiert | seo-geek - Webseiten gezielt optimieren()
Pingback: Reducing Your Joomla Website’s Load Time | Blog of PB Web Development()
Pingback: How to speed up your blog loading time()
Pingback: How to speed up your blog loading time – multimediaDev()
Pingback: Optimización: Utilizar compresión Gzip | Flash Platform()
Pingback: How To Speed Up Your Blog Loading Time « R@j@. R.K()
Pingback: Compresser et ranger son CSS avec PHP. | Darklg Blog()
Pingback: Useful PHP Components & Tutorials for Everyday Project « UR-Technology()
Pingback: 15+ Useful PHP Components, Techniques and Tutorials()