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

Adding optipng post transformer #692

Merged
merged 5 commits into from
Jan 21, 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
61 changes: 61 additions & 0 deletions Imagine/Filter/PostProcessor/OptiPngPostProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;

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

/**
* Constructor.
*
* @param string $optipngBin Path to the optipng binary
*/
public function __construct($optipngBin = '/usr/bin/optipng')
{
$this->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;
}
}
8 changes: 7 additions & 1 deletion Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@

<!-- 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.filter.post_processor.optipng.class">Liip\ImagineBundle\Imagine\Filter\PostProcessor\OptiPngPostProcessor</parameter>
<parameter key="liip_imagine.optipng.binary">/usr/bin/optipng</parameter>

</parameters>

<services>
Expand Down Expand Up @@ -269,5 +271,9 @@
<argument>%liip_imagine.jpegoptim.binary%</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>
<tag name="liip_imagine.filter.post_processor" post_processor="optipng" />
</service>
</services>
</container>
12 changes: 12 additions & 0 deletions Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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