lakshanjs/jpegmini-sdk

A PHP SDK wrapper for the JPEGmini CLI image optimizer.

dev-main 2025-03-28 07:54 UTC

This package is auto-updated.

Last update: 2025-05-30 09:57:14 UTC


README

A lightweight, production-ready PHP SDK wrapper for the JPEGmini Server CLI. It allows you to optimize JPEG images via command line directly from PHP, with support for resizing, quality settings, metadata removal, and more.

πŸ“¦ Installation

Install using Composer:

composer require lakshanjs/jpegmini-sdk

πŸš€ Quick Start

use LakshanJS\Jpegmini\JpegminiSDK;

$jpegmini = new JpegminiSDK('/usr/local/bin/jpegmini');

$result = $jpegmini->optimize('/path/to/input.jpg', [
    'output' => '/path/to/output.jpg',
    'resize' => '60',
    'quality_mode' => 1,
    'remove_metadata' => true,
    'skip_high_compression' => false
]);

print_r($result);

βš™οΈ Options

Option Type Description
output string File or folder path where the optimized image(s) should be saved. If omitted, JPEGmini saves to the same folder as the input, appending _mini to the filename.
recursive bool When optimizing a folder, determines whether subdirectories should be processed. Default: true
logfile string Path to save JPEGmini logs (same as CLI -logfile)
log_level int CLI log verbosity: 0 (none), 1 (basic), 2 (verbose). Default: 1
csvfile string Path to save CSV summary of optimization results
resize string Resize before optimization. Format: '50' (percentage) or '800x600' (fixed size)
quality_mode int Output quality: 0 (best), 1 (high), 2 (medium). Default: 0
skip_high_compression bool Skips images already heavily compressed. Default: true
remove_metadata bool Removes EXIF and other metadata from JPEG files. Default: false

πŸ“ Examples

βž• Optimize and Save with New Name

$jpegmini->optimize('/images/photo.jpg', [
    'output' => '/images/photo_compressed.jpg',
]);

πŸ”„ Overwrite Original File (⚠️ Destructive)

$jpegmini->optimize('/images/photo.jpg', [
    'output' => '/images/photo.jpg'
]);

🧩 Resize & Remove Metadata

$jpegmini->optimize('/images/original.jpg', [
    'resize' => '800x600',
    'remove_metadata' => true
]);

πŸ“ Optimize Entire Folder (Recursively)

$jpegmini->optimize('/images/uploads', [
    'recursive' => true
]);

βœ… Requirements

  • PHP 7.4 or higher
  • JPEGmini Server installed and accessible via CLI
  • Executable binary path (e.g. /usr/local/bin/jpegmini)

πŸ“„ License

MIT License β€” free for commercial and personal use.

πŸ™‹β€β™‚οΈ Author

Lakshan Jayasinghe
GitHub @lakshanjs

πŸ“¬ Contributing

Pull requests and suggestions are welcome! If you want to add Laravel integration, batch queue support, or auto-backup before overwrite, feel free to fork or raise an issue.

πŸ’‘ Tip

This SDK doesn’t use any HTTP calls or external services β€” it's a direct wrapper around the JPEGmini CLI. It’s ideal for secure, offline image processing systems.