From 30ee8efe5d67c2c6839f812bd47797eb8a406bd2 Mon Sep 17 00:00:00 2001 From: Benjamin Hubert Date: Fri, 8 Jan 2016 19:09:22 +0100 Subject: [PATCH 1/5] creating optipng post processor --- .../Filter/PostProcessor/OptiPngPostProcessor | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Imagine/Filter/PostProcessor/OptiPngPostProcessor diff --git a/Imagine/Filter/PostProcessor/OptiPngPostProcessor b/Imagine/Filter/PostProcessor/OptiPngPostProcessor new file mode 100644 index 000000000..c85fac2e1 --- /dev/null +++ b/Imagine/Filter/PostProcessor/OptiPngPostProcessor @@ -0,0 +1,62 @@ +optipngBin = $optipngBin; + } + + /** + * @param BinaryInterface $binary + * + * @throws ProcessFailedException + * + * @return BinaryInterface + * + * @see Implementation taken from Assetic\Filter\optipngFilter + */ + public function process(BinaryInterface $binary) + { + $type = strtolower($binary->getMimeType()); + if (!in_array($type, array('image/png'))) { + return $binary; + } + + $pb = new ProcessBuilder(array($this->optipngBin)); + + $pb->add('--o7'); + $pb->add($input = tempnam(sys_get_temp_dir(), 'imagine_optipng')); + file_put_contents($input, $binary->getContent()); + + $proc = $pb->getProcess(); + $proc->run(); + + if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) { + unlink($input); + throw new ProcessFailedException($proc); + } + + $result = new Binary(file_get_contents($input), $binary->getMimeType(), $binary->getFormat()); + + unlink($input); + + return $result; + } +} From 2fb52fdfd66469a3455a5f73acf5cab5615f1d2b Mon Sep 17 00:00:00 2001 From: Benjamin Hubert Date: Fri, 8 Jan 2016 19:10:47 +0100 Subject: [PATCH 2/5] Update imagine.xml for optipng support --- Resources/config/imagine.xml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index dc4928539..056f002c7 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -66,9 +66,11 @@ Liip\ImagineBundle\Imagine\Filter\PostProcessor\JpegOptimPostProcessor - /usr/bin/jpegoptim + Liip\ImagineBundle\Imagine\Filter\PostProcessor\OptiPngPostProcessor + /usr/bin/optipng + @@ -269,5 +271,9 @@ %liip_imagine.jpegoptim.binary% + + %liip_imagine.optipng.binary% + + From 5324ebfce152dabaa8954feb534ac55b79288d54 Mon Sep 17 00:00:00 2001 From: Benjamin HUBERT Date: Wed, 20 Jan 2016 14:56:15 +0100 Subject: [PATCH 3/5] Fixing class name typo --- .../{OptiPngPostProcessor => OptiPngPostProcessor.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Imagine/Filter/PostProcessor/{OptiPngPostProcessor => OptiPngPostProcessor.php} (100%) diff --git a/Imagine/Filter/PostProcessor/OptiPngPostProcessor b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php similarity index 100% rename from Imagine/Filter/PostProcessor/OptiPngPostProcessor rename to Imagine/Filter/PostProcessor/OptiPngPostProcessor.php From 1934b1ead687b275b0a7cdf4aa44c71b899b0527 Mon Sep 17 00:00:00 2001 From: Benjamin HUBERT Date: Wed, 20 Jan 2016 15:14:55 +0100 Subject: [PATCH 4/5] Removing an useless namespace --- Imagine/Filter/PostProcessor/OptiPngPostProcessor.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php index c85fac2e1..4227f11a0 100644 --- a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php +++ b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php @@ -3,7 +3,6 @@ namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor; use Liip\ImagineBundle\Binary\BinaryInterface; -use Liip\ImagineBundle\Imagine\Filter\PostProcessor\PostProcessorInterface; use Liip\ImagineBundle\Model\Binary; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\ProcessBuilder; From d81660717c0d2ef92fbe1c7cdc15c1ea09ccd718 Mon Sep 17 00:00:00 2001 From: Benjamin Hubert Date: Wed, 20 Jan 2016 21:26:08 +0100 Subject: [PATCH 5/5] Update filters.rst --- Resources/doc/filters.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Resources/doc/filters.rst b/Resources/doc/filters.rst index 7307d1119..bf7e5e30d 100644 --- a/Resources/doc/filters.rst +++ b/Resources/doc/filters.rst @@ -380,3 +380,15 @@ parameters, for example: liip_imagine.jpegoptim.binary: /usr/local/bin/jpegoptim .. _`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 + + parameters: + liip_imagine.optipng.binary: /usr/local/bin/optipng + +.. _`Symfony Service Container`: http://symfony.com/doc/current/book/service_container.html