|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
//namespace WebPConvert\Convert\BaseConverters\BaseTraits; |
|
4
|
|
|
namespace WebPConvert\Convert\Converters\ConverterTraits; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Trait for converters that supports lossless encoding and thus the "lossless:auto" option. |
|
8
|
|
|
* |
|
9
|
|
|
* @package WebPConvert |
|
10
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
11
|
|
|
* @since Class available since Release 2.0.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
trait LosslessAutoTrait |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
public function supportsLossless() |
|
17
|
|
|
{ |
|
18
|
|
|
return true; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** Default is to not pass "lossless:auto" on, but implement it. |
|
22
|
|
|
* |
|
23
|
|
|
* The Stack converter passes it on (it does not even use this trait) |
|
24
|
|
|
* WPC currently implements it, but this might be configurable in the future. |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
public function passOnLosslessAuto() |
|
28
|
|
|
{ |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function runActualConvert() |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$this->passOnLosslessAuto() && ($this->options['lossless'] === 'auto') && $this->supportsLossless()) { |
|
35
|
|
|
$destination = $this->destination; |
|
36
|
|
|
$destinationLossless = $this->destination . '.lossless.webp'; |
|
37
|
|
|
$destinationLossy = $this->destination . '.lossy.webp'; |
|
38
|
|
|
|
|
39
|
|
|
$this->logLn( |
|
|
|
|
|
|
40
|
|
|
'Lossless is set to auto. Converting to both lossless and lossy and selecting the smallest file' |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$this->ln(); |
|
|
|
|
|
|
45
|
|
|
$this->logLn('Converting to lossy'); |
|
46
|
|
|
$this->destination = $destinationLossy; |
|
|
|
|
|
|
47
|
|
|
$this->options['lossless'] = false; |
|
|
|
|
|
|
48
|
|
|
$this->doActualConvert(); |
|
|
|
|
|
|
49
|
|
|
$this->logLn('Reduction: ' . |
|
50
|
|
|
round((filesize($this->source) - filesize($this->destination))/filesize($this->source) * 100) . '% '); |
|
51
|
|
|
|
|
52
|
|
|
$this->ln(); |
|
53
|
|
|
$this->logLn('Converting to lossless'); |
|
54
|
|
|
$this->destination = $destinationLossless; |
|
55
|
|
|
$this->options['lossless'] = true; |
|
56
|
|
|
$this->doActualConvert(); |
|
57
|
|
|
$this->logLn('Reduction: ' . |
|
58
|
|
|
round((filesize($this->source) - filesize($this->destination))/filesize($this->source) * 100) . '% '); |
|
59
|
|
|
|
|
60
|
|
|
$this->ln(); |
|
61
|
|
|
if (filesize($destinationLossless) > filesize($destinationLossy)) { |
|
62
|
|
|
$this->logLn('Picking lossy'); |
|
63
|
|
|
unlink($destinationLossless); |
|
64
|
|
|
rename($destinationLossy, $destination); |
|
65
|
|
|
} else { |
|
66
|
|
|
$this->logLn('Picking lossless'); |
|
67
|
|
|
unlink($destinationLossy); |
|
68
|
|
|
rename($destinationLossless, $destination); |
|
69
|
|
|
} |
|
70
|
|
|
$this->destination = $destination; |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->doActualConvert(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|