Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Add precompiled binaries + composer install instructions #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
composer.phar
# /vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,58 @@ The image optimization is performed using the Spatie library - https://github.co

## Installation

Download and unpack add-on to `<cockpit-folder>/addons/ImageOptimizer` folder.
By default the Spatie library will detect any optimization binaries on your system and use them, more details about that on https://github.com/spatie/image-optimizer#optimization-tools.
### Manual

Download [latest release](https://github.com/pauloamgomes/CockpitCMS-ImageOptimizer) and extract to `COCKPIT_PATH/addons/ImageOptimizer` directory

### Git

```sh
git clone https://github.com/pauloamgomes/CockpitCMS-ImageOptimizer.git ./addons/ImageOptimizer
```

### Cockpit CLI

```sh
php ./cp install/addon --name ImageOptimizer --url https://github.com/pauloamgomes/CockpitCMS-ImageOptimizer.git
```

### Composer

1. Make sure path to cockpit addons is defined in your projects' _composer.json_ file:

```json
{
"name": "MY_PROJECT",
"extra": {
"installer-paths": {
"cockpit/addons/{$name}": ["type:cockpit-module"]
}
}
}
```

2. In your project root run:

```sh
composer require pauloamgomes/cockpitcms-imageoptimizer
```

---

## Configuration

By default the Spatie library will use these optimization binaries if they are present on your system:

- [JpegOptim](http://freecode.com/projects/jpegoptim)
- [Optipng](http://optipng.sourceforge.net/)
- [Pngquant 2](https://pngquant.org/)
- [SVGO](https://github.com/svg/svgo)
- [Gifsicle](http://www.lcdf.org/gifsicle/)
- [cwebp](https://developers.google.com/speed/webp/docs/precompiled)

More details about that on https://github.com/spatie/image-optimizer#optimization-tools.

No additional configuration is required.

## Usage
Expand Down
74 changes: 72 additions & 2 deletions actions.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
<?php

use Spatie\ImageOptimizer\OptimizerChain;

use Spatie\ImageOptimizer\Optimizers\Cwebp;
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
use Spatie\ImageOptimizer\Optimizers\Optipng;
use Spatie\ImageOptimizer\Optimizers\Pngquant;
use Spatie\ImageOptimizer\Optimizers\Svgo;

$BIN_PATH = str_replace(DIRECTORY_SEPARATOR, '/', __DIR__).'/binaries';

$jpegQuality = '--max=85';
$pngQuality = '--quality=85';

/**
* Create custom optimization chain
*
* @see https://github.com/spatie/image-optimizer#creating-your-own-optimization-chains
*/
$spatie = new OptimizerChain();
$jpegoptim = new Jpegoptim([ $jpegQuality, '--strip-all', '--all-progressive']);
$pngquant = new Pngquant([ $pngQuality, '--force' ]);
$optipng = new Optipng([ '-i0', '-o2', '-quiet' ]);
$svgo = new Svgo([ '--disable={cleanupIDs,removeViewBox}' ]);
$gifsicle = new Gifsicle([ '-b', '-O3', ]);
$cwebp = new Cwebp([ '-m 6', '-pass 10', '-mt', '-q 80' ]);

// helper function to detect necessary executables
$where = function ($command) {
return is_executable(shell_exec((strpos(PHP_OS, 'WIN') === 0 ? 'where ' : 'command -v ').$command));
};

switch(PHP_OS) {
case 'Darwin':
case 'FreeBSD':
case 'Linux':
case 'SunOS':
case 'WINNT':

// path to precompiled binaries
$BIN_PATH .= '/'.PHP_OS;

// append ".exe" for windows binaries
if (PHP_OS === 'WINNT') {
$jpegoptim->binaryName .= '.exe';
$pngquant ->binaryName .= '.exe';
$optipng ->binaryName .= '.exe';
$svgo ->binaryName .= '.exe';
$gifsicle ->binaryName .= '.exe';
$cwebp ->binaryName .= '.exe';
}

foreach ([$jpegoptim, $pngquant, $optipng, $svgo, $gifsicle, $cwebp] as $optimizer) {

// if installed, use system binaries
if ($where($optimizer->binaryName)) {
$spatie ->addOptimizer($optimizer);
}

// if bundled, use precompiled binaries
else if (is_executable("$BIN_PATH/$optimizer->binaryName")) {
$optimizer->setBinaryPath($BIN_PATH);
$spatie ->addOptimizer($optimizer);
}

}

break;

}

/**
* Implements cockpit.asset.upload event.
*/
$app->on('cockpit.asset.upload', function(&$asset, &$_meta, &$opts) {
$app->on('cockpit.asset.upload', function(&$asset, &$_meta, &$opts) use($spatie) {
$file = $this->path("#tmp:") . '/' . $asset['title'];
\Spatie\ImageOptimizer\OptimizerChainFactory::create()->optimize($file);
$spatie->optimize($file);
$asset['size'] = filesize($file);
});
Binary file added binaries/Darwin/cwebp
Binary file not shown.
Binary file added binaries/Darwin/gifsicle
Binary file not shown.
Binary file added binaries/Darwin/jpegoptim
Binary file not shown.
Binary file added binaries/Darwin/optipng
Binary file not shown.
Binary file added binaries/Darwin/pngquant
Binary file not shown.
Binary file added binaries/Darwin/svgo
Binary file not shown.
Binary file added binaries/FreeBSD/cwebp
Binary file not shown.
Binary file added binaries/FreeBSD/gifsicle
Binary file not shown.
Binary file added binaries/FreeBSD/optipng
Binary file not shown.
Binary file added binaries/FreeBSD/pngquant
Binary file not shown.
Binary file added binaries/Linux/cwebp
Binary file not shown.
Binary file added binaries/Linux/gifsicle
Binary file not shown.
Binary file added binaries/Linux/jpegoptim
Binary file not shown.
Binary file added binaries/Linux/optipng
Binary file not shown.
Binary file added binaries/Linux/pngquant
Binary file not shown.
Binary file added binaries/Linux/svgo
Binary file not shown.
Binary file added binaries/SunOS/cwebp
Binary file not shown.
Binary file added binaries/SunOS/gifsicle
Binary file not shown.
Binary file added binaries/SunOS/optipng
Binary file not shown.
Binary file added binaries/SunOS/pngquant
Binary file not shown.
Binary file added binaries/WINNT/cwebp.exe
Binary file not shown.
Binary file added binaries/WINNT/gifsicle.exe
Binary file not shown.
Binary file added binaries/WINNT/jpegoptim.exe
Binary file not shown.
Binary file added binaries/WINNT/optipng.exe
Binary file not shown.
Binary file added binaries/WINNT/pngquant.exe
Binary file not shown.
Binary file added binaries/WINNT/svgo.exe
Binary file not shown.
10 changes: 10 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php
/**
* Cockpit image optimizer addon
*
* @author Paulo Gomes
* @package CockpitCMS-ImageOptimizer
* @license MIT
*
* @source https://github.com/pauloamgomes/CockpitCMS-ImageOptimizer
* @see { README.md } for usage info.
*/

require __DIR__ . '/vendor/autoload.php';

Expand Down
14 changes: 11 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "pauloamgomes/CockpitCMS-ImageOptimizer",
"type": "project",
"name": "pauloamgomes/cockpitcms-imageoptimizer",
"description": "Simple Cockpit CMS addon that integrates with Spatie image optimization library.",
"keywords": ["cms", "headless", "api", "cockpit", "images", "media"],
"homepage": "https://github.com/pauloamgomes/CockpitCMS-ImageOptimizer",
"license": "MIT",
"type": "cockpit-module",
"authors": [
{
"name": "Paulo Gomes",
Expand All @@ -13,6 +13,14 @@
}
],
"require": {
"spatie/image-optimizer": "^1.2"
"php": ">= 7.3",
"spatie/image-optimizer": "^1.2",
"composer/installers": "^1.10"
},
"suggest": {
"aheinze/cockpit": "Please install Cockpit before installing this addon"
},
"extra": {
"installer-name": "ImageOptimizer"
}
}
Loading