Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable configuration of post processors using parameters (closes #720) #759

Merged
merged 2 commits into from
Jul 22, 2016
Merged
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
5 changes: 4 additions & 1 deletion Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ class JpegOptimPostProcessor implements PostProcessorInterface, ConfigurablePost
* 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', $stripAll = true, $max = 100, $progressive = true)
public function __construct($jpegoptimBin = '/usr/bin/jpegoptim', $stripAll = true, $max = null, $progressive = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this change intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just looking at this myself during the rebase - it's originally from here 81ee48a#diff-9cd4d6a634315c7cf9b3483cb8b37db8R45 so I chose to keep it.

Setting --max disables lossless mode and will reduce the quality factor of jpegs (if greater than its current value) to the current setting - My assumption then is that it doesn't matter, as 100 is the max quality factor for the jpeg format anyway, so removing the flag shouldn't matter much.

I can run a few test cases using jpegoptim by hand to see if there's any difference, if that'd be preferred?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I guess defaulting to null is better then

{
$this->jpegoptimBin = $jpegoptimBin;
$this->stripAll = $stripAll;
Expand Down
37 changes: 33 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 @@ -44,7 +64,16 @@ 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'));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my late comment, but I have noticed that there is merge conflict. $input gets added twice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, nice catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do a follow up PR?

$pb->add($input);

if ($binary instanceof FileBinaryInterface) {
Expand Down
10 changes: 10 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,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>

<parameter key="liip_imagine.filter.post_processor.pngquant.class">Liip\ImagineBundle\Imagine\Filter\PostProcessor\PngquantPostProcessor</parameter>
<parameter key="liip_imagine.pngquant.binary">/usr/bin/pngquant</parameter>
Expand Down Expand Up @@ -294,10 +299,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>
<service id="liip_imagine.filter.post_processor.pngquant" class="%liip_imagine.filter.post_processor.pngquant.class%">
Expand Down
37 changes: 34 additions & 3 deletions Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,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 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 @@ -413,6 +430,20 @@ Make sure that optipng binary is installed on the system and change the

.. _`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


The ``MozJpegPostProcessor`` can be used to provide safe lossy JPEG optimization.
Optionally, a quality parameter may be passed down to each instance.
Expand Down Expand Up @@ -445,7 +476,7 @@ Make sure that you have installed the mozjpeg tools on your system, and please a


The ``PngquantPostProcessor`` can be used to provide safe lossy PNG optimization.
Optionally, a quality parameter may be passed down to each instance.
Optionally, a quality parameter may be passed down to each instance.
More parameters may surface in the future.

.. code-block:: yaml
Expand Down
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