o2system /
image
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Image; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use ImageOptimizer\OptimizerFactory; |
||
| 19 | use O2System\Image\Optimizers\Imageoptim; |
||
| 20 | use O2System\Image\Optimizers\Optimus; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Class Optimizer |
||
| 24 | * @package O2System\Image |
||
| 25 | */ |
||
| 26 | class Optimizer |
||
| 27 | {
|
||
| 28 | /** |
||
| 29 | * Optimizer::$imageFactory |
||
| 30 | * |
||
| 31 | * @var Imageoptim|Optimus |
||
| 32 | */ |
||
| 33 | protected $imageFactory = null; |
||
| 34 | |||
| 35 | // ------------------------------------------------------------------------ |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Optimizer::setImageFactory |
||
| 39 | * |
||
| 40 | * @param $imageFactory |
||
| 41 | * |
||
| 42 | * @return static |
||
| 43 | */ |
||
| 44 | public function setImageFactory($imageFactory) |
||
| 45 | {
|
||
| 46 | if ($imageFactory instanceof Imageoptim || $imageFactory instanceof Optimus) {
|
||
| 47 | $this->imageFactory = $imageFactory; |
||
| 48 | } |
||
| 49 | |||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | // ------------------------------------------------------------------------ |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Optimizer::optimize |
||
| 57 | * |
||
| 58 | * @param string $imageFilePath |
||
| 59 | * |
||
| 60 | * @throws \Exception |
||
| 61 | */ |
||
| 62 | public function optimize($imageFilePath) |
||
| 63 | {
|
||
| 64 | if (empty($this->imageFactory)) {
|
||
| 65 | if (class_exists('\ImageOptimizer\OptimizerFactory')) {
|
||
| 66 | (new OptimizerFactory())->get()->optimize($imageFilePath); |
||
| 67 | } |
||
| 68 | } elseif ($this->imageFactory instanceof Imageoptim || $this->imageFactory instanceof Optimus) {
|
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 69 | $imageString = $this->imageFactory->optimize($imageFilePath, 'full'); |
||
| 70 | file_put_contents($imageFilePath, $imageString); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |