|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebPConvert\Serve; |
|
3
|
|
|
|
|
4
|
|
|
use WebPConvert\WebPConvert; |
|
5
|
|
|
|
|
6
|
|
|
use WebPConvert\Serve\DecideWhatToServe; |
|
7
|
|
|
use WebPConvert\Serve\Header; |
|
8
|
|
|
use WebPConvert\Serve\Exceptions\ServeFailedException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Serve a freshly converted image. |
|
12
|
|
|
* |
|
13
|
|
|
* @package WebPConvert |
|
14
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
15
|
|
|
* @since Class available since Release 2.0.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class ServeFreshConversion |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
3 |
|
public static function serve($source, $destination, $options = []) |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
try { |
|
24
|
3 |
|
WebPConvert::convert($source, $destination, $options); |
|
25
|
|
|
|
|
26
|
|
|
// We are here, so it was successful :) |
|
27
|
|
|
// If destination is smaller than source, we should serve destination. Otherwise we should serve source. |
|
28
|
|
|
// We can use DecideWhatToServe for that purpose. |
|
29
|
|
|
// However, we must make sure it does not answer "fresh-conversion" or "report" |
|
30
|
|
|
|
|
31
|
|
|
// Unset "reconvert", so we do not get "fresh-conversion" |
|
32
|
2 |
|
unset($options['reconvert']); |
|
33
|
|
|
|
|
34
|
|
|
// Unset "show-report", so we do not get "report" |
|
35
|
2 |
|
unset($options['show-report']); |
|
36
|
|
|
|
|
37
|
2 |
|
list($whatToServe, $whyToServe, $msg) = DecideWhatToServe::decide($source, $destination, $options); |
|
38
|
|
|
|
|
39
|
|
|
switch ($whatToServe) { |
|
40
|
2 |
|
case 'source': |
|
41
|
1 |
|
Header::addHeader('X-WebP-Convert-Action: ' . $msg); |
|
42
|
1 |
|
ServeConvertedWebP::serveOriginal($source, $options); |
|
43
|
1 |
|
break; |
|
44
|
1 |
|
case 'destination': |
|
45
|
1 |
|
ServeConvertedWebP::serveDestination($destination, $options); |
|
46
|
1 |
|
break; |
|
47
|
|
|
|
|
48
|
|
|
case 'fresh-conversion': |
|
49
|
|
|
// intentional fall through |
|
50
|
|
|
case 'report': |
|
51
|
|
|
// intentional fall through |
|
52
|
|
|
default: |
|
53
|
|
|
throw new ServeFailedException( |
|
54
|
|
|
'DecideWhatToServe was supposed to return either "source" or "destination". ' . |
|
55
|
|
|
'However, it returned: "' . $whatToServe . '"' |
|
56
|
|
|
); |
|
57
|
2 |
|
break; |
|
58
|
|
|
} |
|
59
|
1 |
|
} catch (\Exception $e) { |
|
60
|
1 |
|
throw $e; |
|
61
|
|
|
} |
|
62
|
2 |
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|