OpenSencillo  2015.009
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
minify.css.mincss.php
1 <?php
14 class css
15 {
16  /* Add your CSS files to this array (THESE ARE ONLY EXAMPLES) */
17  protected $cssFiles = array();
18 
24  final public function add($path)
25  {
26  $this->cssFiles[] = $path;
27  }
33  final public function minify()
34  {
35  $buffer = "";
36  foreach ($this->cssFiles as $cssFile) {
37  $buffer .= file_get_contents($cssFile);
38  }
39 
40  // Remove comments
41  $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
42 
43  // Remove space after colons
44  $buffer = str_replace(': ', ':', $buffer);
45 
46  // Remove whitespace
47  $buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer);
48 
49  // Collapse adjacent spaces into a single space
50  $buffer = ereg_replace(" {2,}", ' ',$buffer);
51 
52  // Remove spaces that might still be left where we know they aren't needed
53  $buffer = str_replace(array('} '), '}', $buffer);
54  $buffer = str_replace(array('{ '), '{', $buffer);
55  $buffer = str_replace(array('; '), ';', $buffer);
56  $buffer = str_replace(array(', '), ',', $buffer);
57  $buffer = str_replace(array(' }'), '}', $buffer);
58  $buffer = str_replace(array(' {'), '{', $buffer);
59  $buffer = str_replace(array(' ;'), ';', $buffer);
60  $buffer = str_replace(array(' ,'), ',', $buffer);
61 
62  // Enable GZip encoding.
63  ob_start("ob_gzhandler");
64 
65  // Enable caching
66  header('Cache-Control: public');
67 
68  // Expire in one day
69  header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
70 
71  // Set the correct MIME type, because Apache won't set it for us
72  header("Content-type: text/css");
73 
74  // Write everything out
75  return $buffer;
76  }
77 
82  final public function call()
83  {
84  return '<link rel="stylesheet" type="text/css" media="screen, print, projection" href="/css/compressed.css.php" />';
85  }
86 }
87 ?>