3 ways to compress CSS files using PHP

3 ways to compress CSS files using PHP

Posted by Jean-Baptiste Jung on Dec 19, 2008 | 60 comments

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.

Author : Jean-Baptiste Jung

Jean is a 27 years old self-taught web developer and WordPress specialist who lives in Wallonia, the French speaking part of Belgium. In addition to Cats Who Code, he also blogs about WordPress at WpRecipes and about Photoshop at PsdVibe.

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.

60 reactions

» Comments RSS Feed

Trackbacks

  1. PHP ile CSS Dosyalarınızı Sıkıştırmak için 3 Farklı Yöntem | Barış Yüksel | brsyuksel
  2. Free Premium Social Media WordPress Theme: WicketPixie » Wordpress themes marketplace
  3. Wordpress Blog Services - 2008 Most Popular Design posts, Tutorials and Resources
  4. 2008年最佳Web设计文章、教程及资源 - 刀客征途
  5. 3 ways to compress CSS files using PHP | CNSQ Online Blog
  6. 3 ways to compress CSS files using PHP | Urban Mainframe
  7. 2008 Most Popular Design posts, Tutorials and Resources | Web Hosting and Domains
  8. 2008 Most Popular Design posts, Tutorials and Resources | SulVision
  9. Have to say » Blog Archive » 50个Web开发设计文章、教程及资源
  10. tutorialand.com
  11. *real me » Bookmarks on 2009-01-19
  12. 2008 Most Popular Design posts, Tutorials and Resources | Web2.0
  13. 20 Useful PHP Components & Tutorials for Everyday Project | Noupe
  14. Wordpress Blog Services - 20 Useful PHP Components &amp; Tutorials for Everyday Project
  15. blog.no-panic.at » links for 2009-03-03
  16. php note» Blog Archive » CSS圧縮転送について
  17. 20 Useful PHP Components & Tutorials for Everyday Project « Hdeya team blog
  18. Angels Picks » Blog Archive » 20 Useful PHP Components & Tutorials for Everyday Project
  19. Comprimir CSS con PHP « Gambitomeister
  20. Blog InterGraphicDESIGNS » Guía para comprimir archivos CSS con PHP?
  21. How To Achive Maximum Website Performance (Part 3) « Sascha Kimmel - Living The Web Experience Since 1996
  22. links for 2009-05-25 | NeXt
  23. WebDev Best Practices – Parte 1 | José Ricardo

Leave a comment