|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor; |
| 4 | + |
| 5 | +use Liip\ImagineBundle\Binary\BinaryInterface; |
| 6 | +use Liip\ImagineBundle\Model\Binary; |
| 7 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 8 | +use Symfony\Component\Process\ProcessBuilder; |
| 9 | + |
| 10 | +/** |
| 11 | + * pngquant post-processor, for optimal, web-safe, lossy png compression |
| 12 | + * This requires a recent version of pngquant (so 2.3 or higher?) |
| 13 | + * See pngqaunt.org if you are unable to find a binary package for your distribution. |
| 14 | + * |
| 15 | + * @see https://pngquant.org/ |
| 16 | + * |
| 17 | + * @author Alex Wilson <[email protected]> |
| 18 | + */ |
| 19 | +class PngquantPostProcessor implements PostProcessorInterface, ConfigurablePostProcessorInterface |
| 20 | +{ |
| 21 | + /** @var string Path to pngquant binary */ |
| 22 | + protected $pngquantBin; |
| 23 | + |
| 24 | + /** @var string Quality to pass to pngquant */ |
| 25 | + protected $quality; |
| 26 | + |
| 27 | + /** |
| 28 | + * Constructor. |
| 29 | + * |
| 30 | + * @param string $pngquantBin Path to the pngquant binary |
| 31 | + */ |
| 32 | + public function __construct($pngquantBin = '/usr/bin/pngquant', $quality = '80-100') |
| 33 | + { |
| 34 | + $this->pngquantBin = $pngquantBin; |
| 35 | + $this->setQuality($quality); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param string $quality |
| 40 | + * |
| 41 | + * @return PngquantPostProcessor |
| 42 | + */ |
| 43 | + public function setQuality($quality) |
| 44 | + { |
| 45 | + $this->quality = $quality; |
| 46 | + |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param BinaryInterface $binary |
| 52 | + * |
| 53 | + * @uses PngquantPostProcessor::processWithConfiguration |
| 54 | + * |
| 55 | + * @throws ProcessFailedException |
| 56 | + * |
| 57 | + * @return BinaryInterface |
| 58 | + */ |
| 59 | + public function process(BinaryInterface $binary) |
| 60 | + { |
| 61 | + return $this->processWithConfiguration($binary, array()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param BinaryInterface $binary |
| 66 | + * @param array $options |
| 67 | + * |
| 68 | + * @throws ProcessFailedException |
| 69 | + * |
| 70 | + * @return BinaryInterface |
| 71 | + */ |
| 72 | + public function processWithConfiguration(BinaryInterface $binary, array $options) |
| 73 | + { |
| 74 | + $type = strtolower($binary->getMimeType()); |
| 75 | + if (!in_array($type, array('image/png'))) { |
| 76 | + return $binary; |
| 77 | + } |
| 78 | + |
| 79 | + $pb = new ProcessBuilder(array($this->pngquantBin)); |
| 80 | + |
| 81 | + // Specify quality. |
| 82 | + $tranformQuality = array_key_exists('quality', $options) ? $options['quality'] : $this->quality; |
| 83 | + $pb->add('--quality'); |
| 84 | + $pb->add($tranformQuality); |
| 85 | + |
| 86 | + // Read to/from stdout to save resources. |
| 87 | + $pb->add('-'); |
| 88 | + $pb->setInput($binary->getContent()); |
| 89 | + |
| 90 | + $proc = $pb->getProcess(); |
| 91 | + $proc->run(); |
| 92 | + |
| 93 | + // 98 and 99 are "quality too low" to compress current current image which, while isn't ideal, is not a failure |
| 94 | + if (!in_array($proc->getExitCode(), array(0, 98, 99))) { |
| 95 | + throw new ProcessFailedException($proc); |
| 96 | + } |
| 97 | + |
| 98 | + $result = new Binary($proc->getOutput(), $binary->getMimeType(), $binary->getFormat()); |
| 99 | + |
| 100 | + return $result; |
| 101 | + } |
| 102 | +} |
0 commit comments