-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #519 from kostiklv/binary-post-processor
Post-processors - handlers to be applied on filtered image binary
- Loading branch information
Showing
12 changed files
with
499 additions
and
9 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
DependencyInjection/Compiler/PostProcessorsCompilerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Compiler pass to register post_processors tagged with liip_imagine.filter.post_processor | ||
* | ||
* @author Konstantin Tjuterev <[email protected]> | ||
*/ | ||
class PostProcessorsCompilerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$tags = $container->findTaggedServiceIds('liip_imagine.filter.post_processor'); | ||
|
||
if (count($tags) > 0 && $container->hasDefinition('liip_imagine.filter.manager')) { | ||
$manager = $container->getDefinition('liip_imagine.filter.manager'); | ||
|
||
foreach ($tags as $id => $tag) { | ||
$manager->addMethodCall('addPostProcessor', array($tag[0]['post_processor'], new Reference($id))); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?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 JpegOptimPostProcessor implements PostProcessorInterface | ||
{ | ||
/** @var string Path to jpegoptim binary */ | ||
protected $jpegoptimBin; | ||
|
||
/** | ||
* If set --strip-all will be passed to jpegoptim | ||
* | ||
* @var bool | ||
*/ | ||
protected $stripAll = true; | ||
|
||
/** | ||
* If set, --max=$value will be passed to jpegoptim | ||
* | ||
* @var int | ||
*/ | ||
protected $max; | ||
|
||
/** | ||
* If set to true --all-progressive will be passed to jpegoptim, otherwise --all-normal will be passed | ||
* | ||
* @var bool | ||
*/ | ||
protected $progressive = true; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $jpegoptimBin Path to the jpegoptim binary | ||
*/ | ||
public function __construct($jpegoptimBin = '/usr/bin/jpegoptim') | ||
{ | ||
$this->jpegoptimBin = $jpegoptimBin; | ||
} | ||
|
||
/** | ||
* @param int $max | ||
* | ||
* @return JpegOptimPostProcessor | ||
*/ | ||
public function setMax($max) | ||
{ | ||
$this->max = $max; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param boolean $progressive | ||
* | ||
* @return JpegOptimPostProcessor | ||
*/ | ||
public function setProgressive($progressive) | ||
{ | ||
$this->progressive = $progressive; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param boolean $stripAll | ||
* | ||
* @return JpegOptimPostProcessor | ||
*/ | ||
public function setStripAll($stripAll) | ||
{ | ||
$this->stripAll = $stripAll; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param BinaryInterface $binary | ||
* | ||
* @throws ProcessFailedException | ||
* | ||
* @return BinaryInterface | ||
* | ||
* @see Implementation taken from Assetic\Filter\JpegoptimFilter | ||
*/ | ||
public function process(BinaryInterface $binary) | ||
{ | ||
$type = strtolower($binary->getMimeType()); | ||
if (!in_array($type, array('image/jpeg', 'image/jpg'))) { | ||
return $binary; | ||
} | ||
|
||
$pb = new ProcessBuilder(array($this->jpegoptimBin)); | ||
|
||
if ($this->stripAll) { | ||
$pb->add('--strip-all'); | ||
} | ||
|
||
if ($this->max) { | ||
$pb->add('--max='.$this->max); | ||
} | ||
|
||
if ($this->progressive) { | ||
$pb->add('--all-progressive'); | ||
} else { | ||
$pb->add('--all-normal'); | ||
} | ||
|
||
$pb->add($input = tempnam(sys_get_temp_dir(), 'imagine_jpegoptim')); | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor; | ||
|
||
use Liip\ImagineBundle\Binary\BinaryInterface; | ||
|
||
/** | ||
* Interface for PostProcessors - handlers which can operate on binaries prepared in FilterManager | ||
* | ||
* @author Konstantin Tjuterev <[email protected]> | ||
*/ | ||
interface PostProcessorInterface | ||
{ | ||
/** | ||
* @param BinaryInterface $binary | ||
* | ||
* @return BinaryInterface | ||
*/ | ||
function process(BinaryInterface $binary); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.