Skip to main content

Compress lib

Utility for compressing (minifying) JavaScript and CSS content as strings or files, with optional combining of multiple sources (local or remote).

Access the library via the shared lib instance:

$compress = $this->lib->compress; // instance of compress_lib

Notes:

  • Supported types: js and css (by file extension when using file()/files()).
  • Remote sources are supported (URLs starting with http, https, or protocol-relative //) via cURL.
  • JavaScript compression is conservative to avoid WinX preg_replace() issues. It removes comment blocks, line comments, tabs, and some redundant spaces.
  • CSS compression removes comments and unnecessary whitespace around tokens like , ;.
  • Errors are raised with trigger_error(E_USER_ERROR) for invalid file types and missing files.

javascript()

Compress a JavaScript string buffer.

  • Parameters: string $buffer
  • Returns: string — minified JavaScript

Behavior highlights:

  • Temporarily replaces http:// and https:// with placeholders to avoid breaking URLs when removing // line comments; then restores them.
  • Iteratively removes the first /* ... */ block until none remain.
  • Strips // line comments, trims lines, removes tabs, and normalizes spaces around =.

Usage Example:

$js = file_get_contents('assets/app.js');
$min = $this->lib->compress->javascript($js);

Caveats:

  • This is a simple (safe) minifier and may not handle all complex JS edge cases (e.g., protocol-relative URLs like //cdn.example.com, or // inside string literals not covered by the temporary protocol replacement). For third‑party bundles, consider using already minified files.

css()

Compress a CSS string buffer.

  • Parameters: string $buffer
  • Returns: string — minified CSS

Behavior:

  • Removes comment blocks /* ... */.
  • Removes tabs, new lines, and multiple spaces.
  • Trims extra spaces around {, }, and ;.

Usage Example:

$css = file_get_contents('assets/site.css');
$min = $this->lib->compress->css($css);
file_put_contents('public/site.min.css', $min);

file()

Compress a single file (local or remote). Type is detected by file extension.

  • Parameters: string $source_file, mixed $destination_file = null
  • Returns: mixed — if $destination_file is provided, returns true after writing; otherwise returns the compressed string content.

Behavior:

  • Type detection via extension of basename($source_file).
  • Remote sources: if $source_file starts with // or http, it is fetched via cURL and then, if the extension is js or css, compressed accordingly.
  • Local sources: validates allowed type (js, css), checks file exists and is readable, then reads and compresses content by type.
  • If $destination_file is not null, writes the result using $this->lib->file->write().

Errors:

  • Invalid type (for local files) triggers E_USER_ERROR.
  • Missing/unreadable file triggers E_USER_ERROR.

Usage Examples:

// Compress and return content as string
$minJs = $this->lib->compress->file('assets/app.js');

// Compress and save to a file
$this->lib->compress->file('assets/app.js', 'public/app.min.js');

// Remote JS (fetched and compressed, returned as string)
$minRemote = $this->lib->compress->file('https://cdn.example.com/lib.js');

// CSS
$this->lib->compress->file('assets/site.css', 'public/site.min.css');

files()

Combine multiple files (local and/or remote) into one string or file, with per‑file compression flags.

  • Parameters: array $source_files, mixed $destination_file = null
  • Returns: mixed — if $destination_file is provided, returns the destination file path; otherwise returns the combined string content.

Input format:

$source_files = [
// 'filename' => true — compress then combine
// 'filename' => false — append as-is (no compression)
'assets/vendor/jquery.js' => false,
'assets/app.js' => true,
'https://cdn.example.com/plugin.js' => true, // remote
'assets/styles.css' => true, // yes, CSS allowed
];

Behavior:

  • For entries marked true, internally calls file($source_file) which handles type detection and compression.
  • For entries marked false, reads the file (local or remote) and appends its content without compression. A leading new line is inserted before non‑compressed inclusions to keep boundaries clear.
  • If $destination_file is null, returns the combined string; otherwise writes to the destination and returns its path.

Usage Examples:

// Build a combined bundle in memory
$bundle = $this->lib->compress->files([
'assets/vendor/jquery.js' => false, // already minified vendor file
'assets/app.js' => true, // compress our code
]);

// Build and save to disk
$out = $this->lib->compress->files([
'assets/vendor/jquery.js' => false,
'assets/app.js' => true,
'assets/styles.css' => true,
], 'public/bundle.txt'); // returns 'public/bundle.txt'

Dependencies and environment

  • Requires the shared lib instance (see: The library instance and usage).
  • Uses $this->lib->file for extension detection and file I/O.
  • Remote fetching uses PHP cURL; ensure the cURL extension is enabled and outbound HTTP/S is allowed.
  • Timeouts and user‑agent string are set internally for remote requests.

Tips

  • Prefer marking already minified vendor files as false in files() to avoid double minification.
  • When working with complex JavaScript that may include tricky comment sequences or protocol‑relative URLs, validate the output or use pre‑minified assets.