Skip to content

Commit

Permalink
Initial release commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gtuk committed Jul 24, 2016
1 parent a9add96 commit 09f2108
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 25 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ImageOptimizer
===========
PHP image optimizer for png, jpeg and gif files. It uses mozjpeg, pngquant and gifsicle for the optimization process.

**This guide assumes you have mozjpeg, pngquant and gifsicle installed.**

Installation
-----------
You can install this library with composer or include it manually in your project.

Quick start
-----------

```php
$optimizer = new Optimizer(
array(
Optimizer::PNGQUANT_PATH => '/usr/local/bin/pngquant',
Optimizer::MOZJPEG_PATH => '/usr/local/bin/cjpeg',
Optimizer::GIFSICLE_PATH => '/usr/local/bin/gifsicle'
)
);
```

After this you can run the optimization process. If the optimization failed the method will throw an Exception, otherwise it returns TRUE.

```php
$optimizer->optimize('example.jpg', 'example-optimized.jpg'));
```

6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
"gifsicle",
"php"
],
"minimum-stability": "dev",
"license": "MIT",
"minimum-stability": "stable",
"require": {
"php": ">=5.3"
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
},
"autoload": {
"psr-0": {
"ImageOptimizer": "src/"
"ImageOptimizer": "src"
}
}
}
10 changes: 7 additions & 3 deletions src/ImageOptimizer/Optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace ImageOptimizer;

use Exception;
use ImageOptimizer\Optimizer\Gifsicle;
use ImageOptimizer\Optimizer\Mozjpeg;
use ImageOptimizer\Optimizer\Pngquant;
use ImageOptimizer\Provider\Optimization\Gifsicle;
use ImageOptimizer\Provider\Optimization\Mozjpeg;
use ImageOptimizer\Provider\Optimization\Pngquant;

/**
* Class Optimizer
Expand Down Expand Up @@ -56,6 +56,8 @@ public function __construct($optimizer = array())
* @param string $input
* @param string $output
*
* @return bool
*
* @throws Exception
*/
public function optimize($input, $output = '')
Expand Down Expand Up @@ -84,5 +86,7 @@ public function optimize($input, $output = '')
if (false === $result) {
throw new Exception('Could not write to file');
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php

namespace ImageOptimizer\Optimizer;
namespace ImageOptimizer\Provider\Optimization;

use Exception;

/**
* Class AbstractImage
* Class AbstractOptimizationProvider
*
* @package ImageOptimizer\Optimizer
* @package ImageOptimizer\Provider\Optimization
*/
abstract class AbstractImage
abstract class AbstractOptimizationProvider
{
protected $binaryPath;

/**
* AbstractImage constructor.
* AbstractOptimizationProvider constructor.
*
* @param string $binaryPath
*/
Expand All @@ -26,7 +28,9 @@ public function __construct($binaryPath)
*
* @param mixed $image
*
* @return void
* @return string
*
* @throws Exception
*/
abstract public function optimize($image);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace ImageOptimizer\Optimizer;
namespace ImageOptimizer\Provider\Optimization;

use Exception;

/**
* Class Gifsicle
*
* @package ImageOptimizer\Optimizer
* @package ImageOptimizer\Provider\Optimization
*/
class Gifsicle extends AbstractImage
class Gifsicle extends AbstractOptimizationProvider
{
/**
* Gifsicle constructor.
Expand All @@ -27,14 +27,15 @@ public function __construct($binaryPath)
* @param mixed $image
*
* @return string
*
* @throws Exception
*/
public function optimize($image)
{
$content = shell_exec($this->binaryPath.' -O2 '.escapeshellarg($image));

if (!$content) {
throw new Exception();
throw new Exception('There was an error during the optimization');
}

return $content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace ImageOptimizer\Optimizer;
namespace ImageOptimizer\Provider\Optimization;

use Exception;

/**
* Class Mozjpeg
*
* @package ImageOptimizer\Optimizer
* @package ImageOptimizer\Provider\Optimization
*/
class Mozjpeg extends AbstractImage
class Mozjpeg extends AbstractOptimizationProvider
{
/**
* Mozjpeg constructor.
Expand All @@ -27,14 +27,15 @@ public function __construct($binaryPath)
* @param mixed $image
*
* @return string
*
* @throws Exception
*/
public function optimize($image)
{
$content = shell_exec($this->binaryPath.' '.escapeshellarg($image));
$content = shell_exec($this->binaryPath.' -optimize '.escapeshellarg($image));

if (!$content) {
throw new Exception();
throw new Exception('There was an error during the optimization');
}

return $content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace ImageOptimizer\Optimizer;
namespace ImageOptimizer\Provider\Optimization;

use Exception;

/**
* Class Pngquant
*
* @package ImageOptimizer\Optimizer
* @package ImageOptimizer\Provider\Optimization
*/
class Pngquant extends AbstractImage
class Pngquant extends AbstractOptimizationProvider
{
/**
* Pngquant constructor.
Expand All @@ -27,14 +27,15 @@ public function __construct($binaryPath)
* @param mixed $image
*
* @return string
*
* @throws Exception
*/
public function optimize($image)
{
$content = shell_exec($this->binaryPath.' - < '.escapeshellarg($image));

if (!$content) {
throw new Exception();
throw new Exception('There was an error during the optimization');
}

return $content;
Expand Down

0 comments on commit 09f2108

Please sign in to comment.