Skip to content

Commit

Permalink
Merge pull request #756 from dylanschoenmakers/master
Browse files Browse the repository at this point in the history
Add configuration options for jpegoptim post-processor
  • Loading branch information
lsmith77 authored Jul 22, 2016
2 parents 6a01756 + ec2df8e commit 72a78bf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
41 changes: 32 additions & 9 deletions Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;

class JpegOptimPostProcessor implements PostProcessorInterface
class JpegOptimPostProcessor implements PostProcessorInterface, ConfigurablePostProcessorInterface
{
/** @var string Path to jpegoptim binary */
protected $jpegoptimBin;
Expand All @@ -18,7 +18,7 @@ class JpegOptimPostProcessor implements PostProcessorInterface
*
* @var bool
*/
protected $stripAll = true;
protected $stripAll;

/**
* If set, --max=$value will be passed to jpegoptim.
Expand All @@ -32,16 +32,19 @@ class JpegOptimPostProcessor implements PostProcessorInterface
*
* @var bool
*/
protected $progressive = true;
protected $progressive;

/**
* Constructor.
*
* @param string $jpegoptimBin Path to the jpegoptim binary
*/
public function __construct($jpegoptimBin = '/usr/bin/jpegoptim')
public function __construct($jpegoptimBin = '/usr/bin/jpegoptim', $stripAll = true, $max = 100, $progressive = true)
{
$this->jpegoptimBin = $jpegoptimBin;
$this->stripAll = $stripAll;
$this->max = $max;
$this->progressive = $progressive;
}

/**
Expand Down Expand Up @@ -83,13 +86,30 @@ public function setStripAll($stripAll)
/**
* @param BinaryInterface $binary
*
* @uses JpegOptimPostProcessor::processWithConfiguration
*
* @throws ProcessFailedException
*
* @return BinaryInterface
*
* @see Implementation taken from Assetic\Filter\JpegoptimFilter
* @see Implementation taken from Assetic\Filter\JpegoptimFilter
*/
public function process(BinaryInterface $binary)
{
return $this->processWithConfiguration($binary, array());
}

/**
* @param BinaryInterface $binary
* @param array $options
*
* @throws ProcessFailedException
*
* @return BinaryInterface
*
* @see Implementation taken from Assetic\Filter\JpegoptimFilter
*/
public function processWithConfiguration(BinaryInterface $binary, array $options)
{
$type = strtolower($binary->getMimeType());
if (!in_array($type, array('image/jpeg', 'image/jpg'))) {
Expand All @@ -102,15 +122,18 @@ public function process(BinaryInterface $binary)

$pb = new ProcessBuilder(array($this->jpegoptimBin));

if ($this->stripAll) {
$stripAll = array_key_exists('strip_all', $options) ? $options['strip_all'] : $this->stripAll;
if ($stripAll) {
$pb->add('--strip-all');
}

if ($this->max) {
$pb->add('--max='.$this->max);
$max = array_key_exists('max', $options) ? $options['max'] : $this->max;
if ($max) {
$pb->add('--max='.$max);
}

if ($this->progressive) {
$progressive = array_key_exists('progressive', $options) ? $options['progressive'] : $this->progressive;
if ($progressive) {
$pb->add('--all-progressive');
} else {
$pb->add('--all-normal');
Expand Down
8 changes: 5 additions & 3 deletions Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,10 @@ For an example of a post processor implementation, refer to
``Liip\ImagineBundle\Imagine\Filter\PostProcessor\JpegOptimPostProcessor``.

The ``JpegOptimPostProcessor`` can be used to provide lossless JPEG
optimization, which is good for you website loading speed. In order to add
lossless JPEG optimization to your filters, use the following configuration:
optimization, which is good for you website loading speed. Parameters to configure
stripping of comment and exif data, max quality and progressive rendering may be
passed in optionally. In order to add lossless JPEG optimization to your filters,
use the following configuration:

.. code-block:: yaml
Expand All @@ -386,7 +388,7 @@ lossless JPEG optimization to your filters, use the following configuration:
filters:
thumbnail: { size: [150, 150], mode: outbound }
post_processors:
jpegoptim: {}
jpegoptim: { strip_all: true, max: 70, progressive: true }
Make sure that jpegoptim binary is installed on the system. If path to jpegoptim
binary is different from ``/usr/bin/jpegoptim``, adjust the path by overriding
Expand Down

0 comments on commit 72a78bf

Please sign in to comment.