css minify / combine / param with PHP & Apache (usefull for all)


/ Published in: PHP
Save to your folder(s)

compress & combine CSS files and param theme with PHP vars


Copy this code and paste it in your HTML
  1. /* in .htaccess */
  2. AddType application/x-httpd-php .css // to parse PHP var
  3.  
  4. /* on root CSS folder */
  5. <?php
  6. ob_start("ob_gzhandler");
  7. ob_start("compress");
  8. Header("Content-type:text/css;charset=utf-8");
  9. function compress($buffer) {
  10. // remove comments
  11. $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  12. // remove tabs, spaces, new lines
  13. $buffer = str_replace(array("
  14. ", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
  15. // remove spaces
  16. $buffer = str_replace('{ ', '{', $buffer);
  17. $buffer = str_replace(' }', '}', $buffer);
  18. $buffer = str_replace('; ', ';', $buffer);
  19. $buffer = str_replace(', ', ',', $buffer);
  20. $buffer = str_replace(' {', '{', $buffer);
  21. $buffer = str_replace('} ', '}', $buffer);
  22. $buffer = str_replace(': ', ':', $buffer);
  23. $buffer = str_replace(' ,', ',', $buffer);
  24. $buffer = str_replace(' ;', ';', $buffer);
  25. return $buffer;
  26. }
  27.  
  28. // include your PHP vars
  29. include 'cssvar.php';
  30.  
  31. // and insert all your CSS files
  32. include 'reset.css';
  33. ?>
  34.  
  35.  
  36. /* in CSSVAR.PHP */
  37. <?php
  38. $v_css_gradient_blue = "
  39. background: #b8c1d7;
  40. background: -webkit-gradient(linear, left top, left bottom, from(#dae3ee), to(#b8c1d7));
  41. background: -moz-linear-gradient(top, #dae3ee, #b8c1d7);
  42. filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#dae3ee', endColorstr='#b8c1d7');
  43. ";
  44. ?>
  45.  
  46.  
  47. /* Use in CSS files */
  48. h1{
  49. padding:0 0 15px 0;
  50. margin:0;
  51. font-size:180%;
  52. <?php echo $v_css_gradient_blue;?>
  53. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.