1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of the Assetic package, an OpenSky project. |
---|
5 | * |
---|
6 | * (c) 2010-2012 OpenSky Project Inc |
---|
7 | * |
---|
8 | * For the full copyright and license information, please view the LICENSE |
---|
9 | * file that was distributed with this source code. |
---|
10 | */ |
---|
11 | |
---|
12 | namespace Assetic\Filter; |
---|
13 | |
---|
14 | use Assetic\Asset\AssetInterface; |
---|
15 | use Assetic\Exception\FilterException; |
---|
16 | use Symfony\Component\Process\ProcessBuilder; |
---|
17 | |
---|
18 | /** |
---|
19 | * Runs assets through jpegtran. |
---|
20 | * |
---|
21 | * @link http://jpegclub.org/jpegtran/ |
---|
22 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
23 | */ |
---|
24 | class JpegtranFilter implements FilterInterface |
---|
25 | { |
---|
26 | const COPY_NONE = 'none'; |
---|
27 | const COPY_COMMENTS = 'comments'; |
---|
28 | const COPY_ALL = 'all'; |
---|
29 | |
---|
30 | private $jpegtranBin; |
---|
31 | private $optimize; |
---|
32 | private $copy; |
---|
33 | private $progressive; |
---|
34 | private $restart; |
---|
35 | |
---|
36 | /** |
---|
37 | * Constructor. |
---|
38 | * |
---|
39 | * @param string $jpegtranBin Path to the jpegtran binary |
---|
40 | */ |
---|
41 | public function __construct($jpegtranBin = '/usr/bin/jpegtran') |
---|
42 | { |
---|
43 | $this->jpegtranBin = $jpegtranBin; |
---|
44 | } |
---|
45 | |
---|
46 | public function setOptimize($optimize) |
---|
47 | { |
---|
48 | $this->optimize = $optimize; |
---|
49 | } |
---|
50 | |
---|
51 | public function setCopy($copy) |
---|
52 | { |
---|
53 | $this->copy = $copy; |
---|
54 | } |
---|
55 | |
---|
56 | public function setProgressive($progressive) |
---|
57 | { |
---|
58 | $this->progressive = $progressive; |
---|
59 | } |
---|
60 | |
---|
61 | public function setRestart($restart) |
---|
62 | { |
---|
63 | $this->restart = $restart; |
---|
64 | } |
---|
65 | |
---|
66 | public function filterLoad(AssetInterface $asset) |
---|
67 | { |
---|
68 | } |
---|
69 | |
---|
70 | public function filterDump(AssetInterface $asset) |
---|
71 | { |
---|
72 | $pb = new ProcessBuilder(array($this->jpegtranBin)); |
---|
73 | |
---|
74 | if ($this->optimize) { |
---|
75 | $pb->add('-optimize'); |
---|
76 | } |
---|
77 | |
---|
78 | if ($this->copy) { |
---|
79 | $pb->add('-copy')->add($this->copy); |
---|
80 | } |
---|
81 | |
---|
82 | if ($this->progressive) { |
---|
83 | $pb->add('-progressive'); |
---|
84 | } |
---|
85 | |
---|
86 | if (null !== $this->restart) { |
---|
87 | $pb->add('-restart')->add($this->restart); |
---|
88 | } |
---|
89 | |
---|
90 | $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_jpegtran')); |
---|
91 | file_put_contents($input, $asset->getContent()); |
---|
92 | |
---|
93 | $proc = $pb->getProcess(); |
---|
94 | $code = $proc->run(); |
---|
95 | unlink($input); |
---|
96 | |
---|
97 | if (0 < $code) { |
---|
98 | throw FilterException::fromProcess($proc)->setInput($asset->getContent()); |
---|
99 | } |
---|
100 | |
---|
101 | $asset->setContent($proc->getOutput()); |
---|
102 | } |
---|
103 | } |
---|