PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.7
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.7
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / webp-convert / src / Convert / Converters / ConverterTraits / EncodingAutoTrait.php

EncodingAutoTrait.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.7, at vendor/rosell-dk/webp-convert/src/Convert/Converters/ConverterTraits/EncodingAutoTrait.php

92 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 log($msg, $style = '');
24 abstract protected function ln();
25 abstract protected function logReduction($source, $destination);
26
27 public function supportsLossless()
28 {
29 return true;
30 }
31
32 /** Default is to not pass "lossless:auto" on, but implement it.
33 *
34 * The Stack converter passes it on (it does not even use this trait)
35 * WPC currently implements it, but this might be configurable in the future.
36 *
37 */
38 public function passOnEncodingAuto()
39 {
40 return false;
41 }
42
43 private function convertTwoAndSelectSmallest()
44 {
45 $destination = $this->getDestination();
46 $destinationLossless = $destination . '.lossless.webp';
47 $destinationLossy = $destination . '.lossy.webp';
48
49 $this->logLn(
50 'Encoding is set to auto - converting to both lossless and lossy and selecting the smallest file'
51 );
52
53 $this->ln();
54 $this->logLn('Converting to lossy');
55 $this->setDestination($destinationLossy);
56 $this->setOption('encoding', 'lossy');
57 $this->doActualConvert();
58 $this->log('Reduction: ');
59 $this->logReduction($this->getSource(), $destinationLossy);
60 $this->ln();
61
62 $this->logLn('Converting to lossless');
63 $this->setDestination($destinationLossless);
64 $this->setOption('encoding', 'lossless');
65 $this->doActualConvert();
66 $this->log('Reduction: ');
67 $this->logReduction($this->getSource(), $destinationLossless);
68 $this->ln();
69
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 protected function runActualConvert()
84 {
85 if (!$this->passOnEncodingAuto() && ($this->getOptions()['encoding'] == 'auto') && $this->supportsLossless()) {
86 $this->convertTwoAndSelectSmallest();
87 } else {
88 $this->doActualConvert();
89 }
90 }
91 }
92