forked from liip/LiipImagineBundle
/
OptiPngPostProcessor.php
130 lines (109 loc) · 3.64 KB
/
OptiPngPostProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor;
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Binary\FileBinaryInterface;
use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class OptiPngPostProcessor implements PostProcessorInterface, ConfigurablePostProcessorInterface
{
/**
* @var string Path to optipng binary
*/
protected $optipngBin;
/**
* If set --oN will be passed to optipng.
*
* @var int
*/
protected $level;
/**
* If set --strip=all will be passed to optipng.
*
* @var bool
*/
protected $stripAll;
/**
* Directory where temporary file will be written.
*
* @var string
*/
protected $tempDir;
/**
* Constructor.
*
* @param string $optipngBin Path to the optipng binary
* @param int $level Optimization level
* @param bool $stripAll Strip metadata objects
* @param string $tempDir Directory where temporary file will be written
*/
public function __construct($optipngBin = '/usr/bin/optipng', $level = 7, $stripAll = true, $tempDir = '')
{
$this->optipngBin = $optipngBin;
$this->level = $level;
$this->stripAll = $stripAll;
$this->tempDir = $tempDir ?: sys_get_temp_dir();
}
/**
* @param BinaryInterface $binary
*
* @throws ProcessFailedException
*
* @return BinaryInterface
*/
public function process(BinaryInterface $binary)
{
return $this->processWithConfiguration($binary, []);
}
/**
* @param BinaryInterface $binary
* @param array $options
*
* @throws ProcessFailedException
*
* @return BinaryInterface|Binary
*/
public function processWithConfiguration(BinaryInterface $binary, array $options)
{
$type = strtolower($binary->getMimeType());
if (!in_array($type, ['image/png'])) {
return $binary;
}
$tempDir = array_key_exists('temp_dir', $options) ? $options['temp_dir'] : $this->tempDir;
if (false === $input = tempnam($tempDir, 'imagine_optipng')) {
throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', $tempDir));
}
$processArguments = [$this->optipngBin];
$level = array_key_exists('level', $options) ? $options['level'] : $this->level;
if ($level !== null) {
$processArguments[] = sprintf('--o%d', $level);
}
$stripAll = array_key_exists('strip_all', $options) ? $options['strip_all'] : $this->stripAll;
if ($stripAll) {
$processArguments[] = '--strip=all';
}
$processArguments[] = $input;
if ($binary instanceof FileBinaryInterface) {
copy($binary->getPath(), $input);
} else {
file_put_contents($input, $binary->getContent());
}
$proc = new Process($processArguments);
$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;
}
}