Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
EncodingAutoTrait
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 doActualConvert
n/a
0 / 0
n/a
0 / 0
0
 getSource
n/a
0 / 0
n/a
0 / 0
0
 getDestination
n/a
0 / 0
n/a
0 / 0
0
 setDestination
n/a
0 / 0
n/a
0 / 0
0
 getOptions
n/a
0 / 0
n/a
0 / 0
0
 setOption
n/a
0 / 0
n/a
0 / 0
0
 logLn
n/a
0 / 0
n/a
0 / 0
0
 log
n/a
0 / 0
n/a
0 / 0
0
 ln
n/a
0 / 0
n/a
0 / 0
0
 logReduction
n/a
0 / 0
n/a
0 / 0
0
 supportsLossless
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 passOnEncodingAuto
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 convertTwoAndSelectSmallest
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
2
 runActualConvert
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3//namespace WebPConvert\Convert\Converters\BaseTraits;
4namespace 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 <it@rosell.dk>
11 * @since      Class available since Release 2.0.0
12 */
13trait 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}