Created
June 15, 2019 02:29
-
-
Save qaharmdz/35e547e62e8bf30063cf45c725e53dd1 to your computer and use it in GitHub Desktop.
A small PHP-Script for minifying CSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// specify your css-files and their order here | |
$cssFiles = array( | |
'normalize.css', 'style.css', 'print.css', 'colorbox.css' | |
); | |
// the file to write the compressed css to | |
$minFileName = 'minified.css'; | |
// thats all, just call this file in your browser and it will | |
// build you a minimized css-file. then just link to this single | |
// file and you're done. | |
/*===========================================================================*/ | |
/** | |
* This function takes a css-string and compresses it, removing | |
* unneccessary whitespace, colons, removing unneccessary px/em | |
* declarations etc. | |
* | |
* @param string $css | |
* @return string compressed css content | |
* @author Steffen Becker | |
*/ | |
function minifyCss($css) { | |
// some of the following functions to minimize the css-output are directly taken | |
// from the awesome CSS JS Booster: https://github.com/Schepp/CSS-JS-Booster | |
// all credits to Christian Schaefer: http://twitter.com/derSchepp | |
// remove comments | |
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css); | |
// backup values within single or double quotes | |
preg_match_all('/(\'[^\']*?\'|"[^"]*?")/ims', $css, $hit, PREG_PATTERN_ORDER); | |
for ($i=0; $i < count($hit[1]); $i++) { | |
$css = str_replace($hit[1][$i], '##########' . $i . '##########', $css); | |
} | |
// remove traling semicolon of selector's last property | |
$css = preg_replace('/;[\s\r\n\t]*?}[\s\r\n\t]*/ims', "}\r\n", $css); | |
// remove any whitespace between semicolon and property-name | |
$css = preg_replace('/;[\s\r\n\t]*?([\r\n]?[^\s\r\n\t])/ims', ';$1', $css); | |
// remove any whitespace surrounding property-colon | |
$css = preg_replace('/[\s\r\n\t]*:[\s\r\n\t]*?([^\s\r\n\t])/ims', ':$1', $css); | |
// remove any whitespace surrounding selector-comma | |
$css = preg_replace('/[\s\r\n\t]*,[\s\r\n\t]*?([^\s\r\n\t])/ims', ',$1', $css); | |
// remove any whitespace surrounding opening parenthesis | |
$css = preg_replace('/[\s\r\n\t]*{[\s\r\n\t]*?([^\s\r\n\t])/ims', '{$1', $css); | |
// remove any whitespace between numbers and units | |
$css = preg_replace('/([\d\.]+)[\s\r\n\t]+(px|em|pt|%)/ims', '$1$2', $css); | |
// shorten zero-values | |
$css = preg_replace('/([^\d\.]0)(px|em|pt|%)/ims', '$1', $css); | |
// constrain multiple whitespaces | |
$css = preg_replace('/\p{Zs}+/ims',' ', $css); | |
// remove newlines | |
$css = str_replace(array("\r\n", "\r", "\n"), '', $css); | |
// Restore backupped values within single or double quotes | |
for ($i=0; $i < count($hit[1]); $i++) { | |
$css = str_replace('##########' . $i . '##########', $hit[1][$i], $css); | |
} | |
return $css; | |
} | |
// execute it | |
$cssContents = ''; | |
foreach($cssFiles as $file) { | |
$cssContents .= file_get_contents($file); | |
} | |
file_put_contents($minFileName, minifyCss($cssContents)); | |
echo '<h1>Done.</h1><p>Use your minified version by putting <link href="fileadmin/templates/css/' | |
. $minFileName . '" rel="stylesheet" />" inside your <head>-tag.</p>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment