123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
-
- /*
- * This file is part of the Assetic package, an OpenSky project.
- *
- * (c) 2010-2012 OpenSky Project Inc
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace Assetic\Filter;
-
- use Assetic\Asset\AssetInterface;
- use Assetic\Util\ProcessBuilder;
-
- /**
- * Runs assets through OptiPNG.
- *
- * @link http://optipng.sourceforge.net/
- * @author Kris Wallsmith <kris.wallsmith@gmail.com>
- */
- class OptiPngFilter implements FilterInterface
- {
- private $optipngBin;
- private $level;
-
- /**
- * Constructor.
- *
- * @param string $optipngBin Path to the optipng binary
- */
- public function __construct($optipngBin = '/usr/bin/optipng')
- {
- $this->optipngBin = $optipngBin;
- }
-
- public function setLevel($level)
- {
- $this->level = $level;
- }
-
- public function filterLoad(AssetInterface $asset)
- {
- }
-
- public function filterDump(AssetInterface $asset)
- {
- $pb = new ProcessBuilder(array($this->optipngBin));
-
- if (null !== $this->level) {
- $pb->add('-o')->add($this->level);
- }
-
- $pb->add('-out')->add($output = tempnam(sys_get_temp_dir(), 'assetic_optipng'));
- unlink($output);
-
- $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_optipng'));
- file_put_contents($input, $asset->getContent());
-
- $proc = $pb->getProcess();
- $code = $proc->run();
-
- if (0 < $code) {
- unlink($input);
- throw new \RuntimeException($proc->getErrorOutput());
- }
-
- $asset->setContent(file_get_contents($output));
-
- unlink($input);
- unlink($output);
- }
- }
|