1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Helpers; |
4
|
|
|
|
5
|
|
|
class Image |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Return image URL to serve based on given parameters. |
9
|
|
|
* |
10
|
|
|
* @param string $url |
11
|
|
|
* @param integer $width |
12
|
|
|
* @param integer $height |
13
|
|
|
* @param string $format |
14
|
|
|
* @param integer $quality |
15
|
|
|
* @param boolean|null $useProgressive |
16
|
|
|
* @param string $fit |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
18 |
|
public static function url( |
20
|
|
|
string $url, |
21
|
|
|
int $width = 0, |
22
|
|
|
int $height = 0, |
23
|
|
|
$format = '', |
24
|
|
|
int $quality = 0, |
25
|
|
|
?bool $useProgressive = null, |
26
|
|
|
$fit = '' |
27
|
|
|
): string { |
28
|
18 |
|
if (empty($url)) { |
29
|
2 |
|
return ''; |
30
|
|
|
} |
31
|
|
|
|
32
|
16 |
|
$imageUrl = ''; |
33
|
|
|
|
34
|
16 |
|
$format = static::detectFormat($format); |
35
|
|
|
|
36
|
16 |
|
collect([ |
37
|
16 |
|
'w' => $width, |
38
|
16 |
|
'h' => $height, |
39
|
16 |
|
'q' => !empty($quality) ? $quality : config('contentful.image.default_quality'), |
40
|
16 |
|
'fm' => config('contentful.image.use_webp') ? $format : null, |
41
|
16 |
|
'fl' => static::detectProgressive($format, $useProgressive) ? 'progressive' : null, |
42
|
16 |
|
'fit' => static::detectFit($fit), |
43
|
|
|
])->filter(function ($value) { |
44
|
16 |
|
return !empty($value); |
45
|
|
|
})->each(function ($value, $key) use (& $imageUrl) { |
46
|
16 |
|
$imageUrl .= $key . '=' . $value . '&'; |
47
|
16 |
|
}); |
48
|
|
|
|
49
|
16 |
|
return static::replaceHosts($url, $imageUrl); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param null|string $url |
54
|
|
|
* @param string $imageUrl |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
16 |
|
protected static function replaceHosts(?string $url, string $imageUrl): string |
58
|
|
|
{ |
59
|
16 |
|
if (str_contains($url, '?')) { |
60
|
6 |
|
$url = explode('?', $url); |
61
|
6 |
|
$url = $url[0]; |
62
|
|
|
} |
63
|
|
|
|
64
|
16 |
|
$searchHosts = config('contentful.image.search_hosts'); |
65
|
16 |
|
$replaceHost = config('contentful.image.replace_host'); |
66
|
16 |
|
if (!empty($searchHosts) && !empty($replaceHost)) { |
67
|
16 |
|
$url = str_replace(explode(',', $searchHosts), $replaceHost, $url); |
68
|
|
|
} |
69
|
|
|
|
70
|
16 |
|
return !empty($url) ? $url . '?' . trim($imageUrl, '&') : ''; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Auto-detect image format to serve (based on browser capability). |
76
|
|
|
* |
77
|
|
|
* @param string $format |
78
|
|
|
* @return string|null |
79
|
|
|
*/ |
80
|
16 |
|
protected static function detectFormat(string $format = ''): ?string |
81
|
|
|
{ |
82
|
|
|
/** @var \Jenssegers\Agent\Agent $agent */ |
83
|
16 |
|
$agent = app('agent'); |
84
|
|
|
|
85
|
16 |
|
if (empty($format)) { |
86
|
16 |
|
$browser = mb_strtolower($agent->browser()); |
87
|
16 |
|
if (($browser === 'chrome') && !$agent->isMobile()) { |
88
|
16 |
|
$format = config('contentful.image.use_webp') ? 'webp' : null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
16 |
|
return $format; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param null|string $fit |
97
|
|
|
* @return null|string |
98
|
|
|
*/ |
99
|
16 |
|
protected static function detectFit(?string $fit = null): ?string |
100
|
|
|
{ |
101
|
|
|
|
102
|
16 |
|
if (empty($fit)) { |
103
|
14 |
|
$fit = 'fill'; |
104
|
|
|
} else { |
105
|
2 |
|
$fit = ($fit !== 'default') ? $fit : null; |
106
|
|
|
} |
107
|
|
|
|
108
|
16 |
|
return $fit; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param null|string $format |
113
|
|
|
* @param bool|null $useProgressive |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
16 |
|
protected static function detectProgressive(?string $format = '', ?bool $useProgressive = null): bool |
117
|
|
|
{ |
118
|
16 |
|
if ($format === 'webp') { |
119
|
4 |
|
$useProgressive = false; |
120
|
|
|
} |
121
|
|
|
|
122
|
16 |
|
if ($useProgressive === null) { |
123
|
10 |
|
$useProgressive = !empty(config('contentful.image.use_progressive')); |
124
|
|
|
} |
125
|
|
|
|
126
|
16 |
|
return $useProgressive; |
127
|
|
|
} |
128
|
|
|
} |