├── .gitignore ├── README.md ├── composer.json └── src └── ImageOptimizer ├── Optimizer.php └── Provider └── Optimization ├── AbstractOptimizationProvider.php ├── Gifsicle.php ├── Mozjpeg.php └── Pngquant.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | /composer.lock 4 | /vendor/ 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ImageOptimizer 2 | =========== 3 | PHP image optimizer for png, jpeg and gif files. It uses mozjpeg, pngquant and gifsicle for the optimization process. 4 | 5 | **This guide assumes you have mozjpeg, pngquant and gifsicle installed.** 6 | 7 | Installation 8 | ----------- 9 | You can install this library with composer or include it manually in your project. 10 | 11 | Quick start 12 | ----------- 13 | 14 | ```php 15 | $optimizer = new Optimizer( 16 | array( 17 | Optimizer::PNGQUANT_PATH => '/usr/local/bin/pngquant', 18 | Optimizer::MOZJPEG_PATH => '/usr/local/bin/cjpeg', 19 | Optimizer::GIFSICLE_PATH => '/usr/local/bin/gifsicle' 20 | ) 21 | ); 22 | ``` 23 | 24 | After this you can run the optimization process. If the optimization failed the method will throw an Exception, otherwise it returns TRUE. 25 | 26 | ```php 27 | $optimizer->optimize('example.jpg', 'example-optimized.jpg')); 28 | ``` 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gtuk/image-optimizer", 3 | "author": "Florian Lambart", 4 | "description": "PHP image optimizer for png, jpeg and gif files. It uses mozjpeg, pngquant and gifsicle for the optimization process", 5 | "type": "library", 6 | "keywords": [ 7 | "image optimizer", 8 | "image", 9 | "optimizer", 10 | "optimize", 11 | "compress", 12 | "compression", 13 | "optimization", 14 | "mozjpeg", 15 | "pngquant", 16 | "gifsicle", 17 | "php" 18 | ], 19 | "minimum-stability": "dev", 20 | "license": "MIT", 21 | "require": { 22 | "php": ">=5.4" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "~4.8" 26 | }, 27 | "autoload": { 28 | "psr-0": { 29 | "ImageOptimizer": "src" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/ImageOptimizer/Optimizer.php: -------------------------------------------------------------------------------- 1 | mozjpeg = new Mozjpeg($optimizer[self::MOZJPEG_PATH]); 49 | $this->pngquant = new Pngquant($optimizer[self::PNGQUANT_PATH]); 50 | $this->gifsicle = new Gifsicle($optimizer[self::GIFSICLE_PATH]); 51 | } 52 | 53 | /** 54 | * Optimize image 55 | * 56 | * @param string $input 57 | * @param string $output 58 | * 59 | * @return bool 60 | * 61 | * @throws Exception 62 | */ 63 | public function optimize($input, $output = '') 64 | { 65 | switch (pathinfo($input, PATHINFO_EXTENSION)) { 66 | case 'jpg': 67 | case 'jpeg': 68 | $content = $this->mozjpeg->optimize($input); 69 | break; 70 | case 'png': 71 | $content = $this->pngquant->optimize($input); 72 | break; 73 | case 'gif': 74 | $content = $this->gifsicle->optimize($input); 75 | break; 76 | default: 77 | throw new Exception('No valid file type'); 78 | } 79 | 80 | if (! empty($output)) { 81 | $result = file_put_contents($output, $content); 82 | } else { 83 | $result = file_put_contents($input, $content); 84 | } 85 | 86 | if (false === $result) { 87 | throw new Exception('Could not write to file'); 88 | } 89 | 90 | return true; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/ImageOptimizer/Provider/Optimization/AbstractOptimizationProvider.php: -------------------------------------------------------------------------------- 1 | binaryPath = $binaryPath; 24 | } 25 | 26 | /** 27 | * Optimize image 28 | * 29 | * @param mixed $image 30 | * 31 | * @return string 32 | * 33 | * @throws Exception 34 | */ 35 | abstract public function optimize($image); 36 | } 37 | -------------------------------------------------------------------------------- /src/ImageOptimizer/Provider/Optimization/Gifsicle.php: -------------------------------------------------------------------------------- 1 | binaryPath.' -O2 '.escapeshellarg($image)); 36 | 37 | if (!$content) { 38 | throw new Exception('There was an error during the optimization'); 39 | } 40 | 41 | return $content; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/ImageOptimizer/Provider/Optimization/Mozjpeg.php: -------------------------------------------------------------------------------- 1 | binaryPath.' -optimize '.escapeshellarg($image)); 36 | 37 | if (!$content) { 38 | throw new Exception('There was an error during the optimization'); 39 | } 40 | 41 | return $content; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/ImageOptimizer/Provider/Optimization/Pngquant.php: -------------------------------------------------------------------------------- 1 | binaryPath.' - < '.escapeshellarg($image)); 36 | 37 | if (!$content) { 38 | throw new Exception('There was an error during the optimization'); 39 | } 40 | 41 | return $content; 42 | } 43 | } 44 | --------------------------------------------------------------------------------