PHP Classes

PHP CSS Optimize: Optimize CSS files removing redundant data

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 203 All time: 8,435 This week: 138Up
Version License PHP version Categories
css-optimize 1.0.2GNU General Publi...5PHP 5, Compression, Performance and o...
Description 

Author

This package can optimize CSS files removing redundant data.

It can take a given CSS file and parse it to be rewritten combining duplicate selectors, extracting duplicate CSS properties and values, minifying the CSS removing white space and comments.

The optimized CSS can be rewritten as CSS or SCSS

Innovation Award
PHP Programming Innovation award nominee
December 2016
Number 6


Prize: PhpStorm IDE 1 year individual subscription
There are many CSS file optimization solutions, but most do not do much more than removing white space and comments.

This package can also optimize CSS files combining duplicated selectors and removing duplicated property values.

Manuel Lemos
Picture of Jelle Sebreghts
  Performance   Level  
Name: Jelle Sebreghts <contact>
Classes: 7 packages by
Country: United States United States
Age: 34
All time rank: 1485223 in United States United States
Week rank: 312 Up38 in United States United States Up
Innovation award
Innovation award
Nominee: 4x

Example

<?php

include_once '../vendor/autoload.php';
$css = <<<EOT
div {
  display:block;
  background: transparent;
}
div .black-with-border {
  border: 1px solid black;
  color: black;
  font-weight: bold;
  text-decoration: underline;
  margin: 0;
}

div .black-with-border-and-margin {
  border: 1px solid black;
  color: black;
  font-weight: bold;
  text-decoration: underline;
  margin: 5px 0;
}

h1 {
  font-weight: bold;
  text-decoration: underline;
  padding: 5px;
  color: #d3d3d3;
  margin: 0;
  text-align: left;
}

h2 {
  font-weight: bold;
  text-decoration: underline;
  padding: 5px;
  color: #d3d3d3;
  margin: 0;
  text-align: right;
}

h3 {
  font-weight: bold;
  text-decoration: underline;
  padding: 5px;
  color: #d3d3d3;
  margin: 3px;
  text-align: right;
}

@media only screen and (min-width:16.5em) {
  div .black-with-border {
    border: 1px solid black;
    color: black;
    font-weight: bold;
    text-decoration: underline;
    margin: 0;
  }

  div .black-with-border-and-margin {
    border: 1px solid black;
    color: black;
    font-weight: bold;
    text-decoration: underline;
    margin: 5px 0;
  }
}

@media only screen and (min-width:36em) {
  div .black-with-border {
    border: 1px solid black;
    color: black;
    font-weight: bold;
    text-decoration: underline;
    margin: 0;
  }

  div .black-with-border-and-margin {
    border: 1px solid black;
    color: black;
    font-weight: bold;
    text-decoration: underline;
    margin: 5px 0;
  }
}
EOT;
$optimizer = new Jelle_S\CssOptimizer\CssOptimizer($css, 3);
file_put_contents('style.min.css', $optimizer->renderMinifiedCSS());


$optimizer = new Jelle_S\CssOptimizer\CssOptimizer($css, 3);
file_put_contents('style.scss', $optimizer->renderSCSS());


Details

CssOptimizer

Optimizes CSS (combines duplicate selectors, extracts duplicate css properties and values, minifies the css).

Render minified CSS:

$optimizer = new Jelle_S\CssOptimizer\CssOptimizer($css);
file_put_contents('style.min.css', $optimizer->renderMinifiedCSS());

Render SCSS from your css:

$optimizer = new Jelle_S\CssOptimizer\CssOptimizer($css);
file_put_contents('style.scss', $optimizer->renderSCSS());

This code is open source (GPL-3.0), however, if you like this class and you would like to sponsor the developer (me), feel free to paypal.


  Files folder image Files  
File Role Description
Files folder imageexample (3 files)
Files folder imagesrc (1 file, 1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file example.php Example Example script
  Accessible without login Plain text file style.min.css Data Auxiliary data
  Accessible without login Plain text file style.scss Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageTree (1 file)
  Plain text file CssOptimizer.php Class Class source

  Files folder image Files  /  src  /  Tree  
File Role Description
  Plain text file CssNode.php Class Class source

Downloadcss-optimize-2020-02-17.zip 17KB
Downloadcss-optimize-2020-02-17.tar.gz 16KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
PHP Array Key Combiner Download .zip .tar.gz Composer dependency. Required
 Version Control Unique User Downloads Download Rankings  
 100%
Total:203
This week:0
All time:8,435
This week:138Up