|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
//namespace WebPConvert\Convert\Converters\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 EncodingAutoTrait |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
abstract protected function doActualConvert(); |
|
17
|
|
|
abstract public function getSource(); |
|
18
|
|
|
abstract public function getDestination(); |
|
19
|
|
|
abstract public function setDestination($destination); |
|
20
|
|
|
abstract public function getOptions(); |
|
21
|
|
|
abstract protected function setOption($optionName, $optionValue); |
|
22
|
|
|
abstract protected function logLn($msg, $style = ''); |
|
23
|
|
|
abstract protected function ln(); |
|
24
|
|
|
|
|
25
|
1 |
|
public function supportsLossless() |
|
26
|
|
|
{ |
|
27
|
1 |
|
return true; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** Default is to not pass "lossless:auto" on, but implement it. |
|
31
|
|
|
* |
|
32
|
|
|
* The Stack converter passes it on (it does not even use this trait) |
|
33
|
|
|
* WPC currently implements it, but this might be configurable in the future. |
|
34
|
|
|
* |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function passOnEncodingAuto() |
|
37
|
|
|
{ |
|
38
|
2 |
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
private function convertTwoAndSelectSmallest() |
|
42
|
|
|
{ |
|
43
|
1 |
|
$destination = $this->getDestination(); |
|
44
|
1 |
|
$destinationLossless = $destination . '.lossless.webp'; |
|
45
|
1 |
|
$destinationLossy = $destination . '.lossy.webp'; |
|
46
|
|
|
|
|
47
|
1 |
|
$this->logLn( |
|
48
|
1 |
|
'Encoding is set to auto - converting to both lossless and lossy and selecting the smallest file' |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
1 |
|
$this->ln(); |
|
52
|
1 |
|
$this->logLn('Converting to lossy'); |
|
53
|
1 |
|
$this->setDestination($destinationLossy); |
|
54
|
1 |
|
$this->setOption('encoding', 'lossy'); |
|
55
|
1 |
|
$this->doActualConvert(); |
|
56
|
|
|
$this->logLn('Reduction: ' . |
|
57
|
|
|
round( |
|
58
|
|
|
(filesize($this->getSource()) - filesize($destinationLossy))/filesize($this->getSource()) * 100 |
|
59
|
|
|
) . '% '); |
|
60
|
|
|
$this->ln(); |
|
61
|
|
|
$this->logLn('Converting to lossless'); |
|
62
|
|
|
$this->setDestination($destinationLossless); |
|
63
|
|
|
$this->setOption('encoding', 'lossless'); |
|
64
|
|
|
$this->doActualConvert(); |
|
65
|
|
|
$this->logLn('Reduction: ' . |
|
66
|
|
|
round( |
|
67
|
|
|
(filesize($this->getSource()) - filesize($destinationLossless))/filesize($this->getSource()) * 100 |
|
68
|
|
|
) . '% '); |
|
69
|
|
|
$this->ln(); |
|
70
|
|
|
if (filesize($destinationLossless) > filesize($destinationLossy)) { |
|
71
|
|
|
$this->logLn('Picking lossy'); |
|
72
|
|
|
unlink($destinationLossless); |
|
73
|
|
|
rename($destinationLossy, $destination); |
|
74
|
|
|
} else { |
|
75
|
|
|
$this->logLn('Picking lossless'); |
|
76
|
|
|
unlink($destinationLossy); |
|
77
|
|
|
rename($destinationLossless, $destination); |
|
78
|
|
|
} |
|
79
|
|
|
$this->setDestination($destination); |
|
80
|
|
|
$this->setOption('encoding', 'auto'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
protected function runActualConvert() |
|
84
|
|
|
{ |
|
85
|
2 |
|
if (!$this->passOnEncodingAuto() && ($this->getOptions()['encoding'] == 'auto') && $this->supportsLossless()) { |
|
86
|
1 |
|
$this->convertTwoAndSelectSmallest(); |
|
87
|
|
|
} else { |
|
88
|
1 |
|
$this->doActualConvert(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|