diff --git a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php new file mode 100644 index 000000000..4227f11a0 --- /dev/null +++ b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php @@ -0,0 +1,61 @@ +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; + } +} diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index ead54cc31..1f678d67c 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -67,9 +67,11 @@ Liip\ImagineBundle\Imagine\Filter\PostProcessor\JpegOptimPostProcessor - /usr/bin/jpegoptim + Liip\ImagineBundle\Imagine\Filter\PostProcessor\OptiPngPostProcessor + /usr/bin/optipng + @@ -275,5 +277,9 @@ %liip_imagine.jpegoptim.binary% + + %liip_imagine.optipng.binary% + + 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