Skip to content

Instantly share code, notes, and snippets.

@My1
Forked from antxd/minify.php
Created October 14, 2019 21:52
Show Gist options
  • Save My1/e21b1675755313331f332a281a2f3f98 to your computer and use it in GitHub Desktop.
Save My1/e21b1675755313331f332a281a2f3f98 to your computer and use it in GitHub Desktop.
A small PHP-Script for minifying CSS
<?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 &lt;link href=&quot;fileadmin/templates/css/'
. $minFileName . '&quot; rel=&quot;stylesheet&quot; /&gt;" inside your &lt;head&gt;-tag.</p>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment