Skip to content

Commit

Permalink
Enable configuration of post processors using parameters
Browse files Browse the repository at this point in the history
Fix for invalid variable name

Move symfony/process component to require section

Adding documentation for new parameters.
  • Loading branch information
Vaidas Lažauskas committed Jul 21, 2016
1 parent 0a7932e commit e978356
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
12 changes: 9 additions & 3 deletions Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,22 @@ class JpegOptimPostProcessor implements PostProcessorInterface
*
* @var bool
*/
protected $progressive = true;
protected $progressive;

/**
* Constructor.
*
* @param string $jpegoptimBin Path to the jpegoptim binary
* @param bool $stripAll Strip all markers from output
* @param int $max Set maximum image quality factor
* @param bool $progressive Force output to be progressive
*/
public function __construct($jpegoptimBin = '/usr/bin/jpegoptim')
public function __construct($jpegoptimBin = '/usr/bin/jpegoptim', $stripAll = true, $max = null, $progressive = true)
{
$this->jpegoptimBin = $jpegoptimBin;
$this->stripAll = $stripAll;
$this->max = $max;
$this->progressive = $progressive;
}

/**
Expand Down
34 changes: 30 additions & 4 deletions Imagine/Filter/PostProcessor/OptiPngPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,37 @@

class OptiPngPostProcessor implements PostProcessorInterface
{
/** @var string Path to optipng binary */
protected $optipng;
/**
* @var string Path to optipng binary
*/
protected $optipngBin;

/**
* If set --oN will be passed to optipng.
*
* @var int
*/
protected $level;

/**
* If set --strip=all will be passed to optipng.
*
* @var bool
*/
protected $stripAll;

/**
* Constructor.
*
* @param string $optipngBin Path to the optipng binary
* @param int $level Optimization level
* @param bool $stripAll Strip metadata objects
*/
public function __construct($optipngBin = '/usr/bin/optipng')
public function __construct($optipngBin = '/usr/bin/optipng', $level = 7, $stripAll = true)
{
$this->optipngBin = $optipngBin;
$this->level = $level;
$this->stripAll = $stripAll;
}

/**
Expand All @@ -41,7 +61,13 @@ public function process(BinaryInterface $binary)

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

$pb->add('--o7');
if ($this->level !== null) {
$pb->add(sprintf('--o%d', $this->level));
}
if ($this->stripAll) {
$pb->add('--strip=all');
}

$pb->add($input = tempnam(sys_get_temp_dir(), 'imagine_optipng'));
if ($binary instanceof FileBinaryInterface) {
copy($binary->getPath(), $input);
Expand Down
12 changes: 11 additions & 1 deletion Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@
<!-- Post processors' classes -->
<parameter key="liip_imagine.filter.post_processor.jpegoptim.class">Liip\ImagineBundle\Imagine\Filter\PostProcessor\JpegOptimPostProcessor</parameter>
<parameter key="liip_imagine.jpegoptim.binary">/usr/bin/jpegoptim</parameter>
<parameter key="liip_imagine.jpegoptim.stripAll">true</parameter>
<parameter key="liip_imagine.jpegoptim.max">null</parameter>
<parameter key="liip_imagine.jpegoptim.progressive">true</parameter>

<parameter key="liip_imagine.filter.post_processor.optipng.class">Liip\ImagineBundle\Imagine\Filter\PostProcessor\OptiPngPostProcessor</parameter>
<parameter key="liip_imagine.optipng.binary">/usr/bin/optipng</parameter>
<parameter key="liip_imagine.optipng.level">7</parameter>
<parameter key="liip_imagine.optipng.stripAll">true</parameter>

</parameters>

Expand Down Expand Up @@ -208,7 +213,7 @@
<argument><!-- will be injected by StreamLoaderFactory --></argument>
<argument><!-- will be injected by StreamLoaderFactory --></argument>
</service>

<service id="liip_imagine.binary.loader.prototype.flysystem" class="%liip_imagine.binary.loader.flysystem.class%" abstract="true">
<argument type="service" id="liip_imagine.extension_guesser" />
<argument><!-- will be injected by FlysystemLoaderFactory --></argument>
Expand Down Expand Up @@ -275,10 +280,15 @@
<!-- Post processors -->
<service id="liip_imagine.filter.post_processor.jpegoptim" class="%liip_imagine.filter.post_processor.jpegoptim.class%">
<argument>%liip_imagine.jpegoptim.binary%</argument>
<argument>%liip_imagine.jpegoptim.stripAll%</argument>
<argument>%liip_imagine.jpegoptim.max%</argument>
<argument>%liip_imagine.jpegoptim.progressive%</argument>
<tag name="liip_imagine.filter.post_processor" post_processor="jpegoptim" />
</service>
<service id="liip_imagine.filter.post_processor.optipng" class="%liip_imagine.filter.post_processor.optipng.class%">
<argument>%liip_imagine.optipng.binary%</argument>
<argument>%liip_imagine.optipng.level%</argument>
<argument>%liip_imagine.optipng.stripAll%</argument>
<tag name="liip_imagine.filter.post_processor" post_processor="optipng" />
</service>
</services>
Expand Down
35 changes: 33 additions & 2 deletions Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,26 @@ parameters, for example:
.. _`Symfony Service Container`: http://symfony.com/doc/current/book/service_container.html

It is also possible to configure other defaults for the conversion process via parameters,
for example:

The ``OptiPngPostProcessor`` is also available by default and can be used just as jpegoptim.
Make sure that optipng binary is installed on the system and change the
.. code-block:: yaml
parameters:
# When true, this passes down --strip-all to jpegoptim, which strips all markers from the output jpeg.
liip_imagine.jpegoptim.stripAll: true
# Sets the maxiumum image quality factor.
liip_imagine.jpegoptim.max: null
# When true, --all-progressive is passed to jpegoptim, which results in the output being a progressive jpeg.
liip_imagine.jpegoptim.progressive: true
.. _`Symfony Service Container`: http://symfony.com/doc/current/book/service_container.html


The ``OptiPngPostProcessor`` is also available by default and can be used just as jpegoptim.
Make sure that optipng binary is installed on the system and change the
``liip_imagine.optipng.binary`` in parameters if needed.

.. code-block:: yaml
Expand All @@ -392,3 +409,17 @@ Make sure that optipng binary is installed on the system and change the
liip_imagine.optipng.binary: /usr/local/bin/optipng
.. _`Symfony Service Container`: http://symfony.com/doc/current/book/service_container.html

It is also possible to configure other defaults for the conversion process via parameters,
for example:

.. code-block:: yaml
parameters:
# When true, this passes down --strip=all to optipng, which removes all metadata from the output image.
liip_imagine.optipng.stripAll: true
# The optimisation level to be used by optipng. Defaults to 7.
liip_imagine.optipng.level: 7
.. _`Symfony Service Container`: http://symfony.com/doc/current/book/service_container.html
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"symfony/finder": "~2.3|~3.0",
"symfony/filesystem": "~2.3|~3.0",
"symfony/options-resolver": "~2.3|~3.0",
"symfony/framework-bundle": "~2.3|~3.0"
"symfony/framework-bundle": "~2.3|~3.0",
"symfony/process": "~2.3|~3.0"
},

"require-dev": {
Expand Down Expand Up @@ -49,7 +50,6 @@
"league/flysystem": "If you'd want to use Flysystem binary loader"
},


"autoload": {
"psr-4": { "Liip\\ImagineBundle\\": "" }
},
Expand Down

0 comments on commit e978356

Please sign in to comment.