|
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
|
|
|
public static function url(string $url, int $width = 0, int $height = 0, $format = '', int $quality = 0, ?bool $useProgressive = null, $fit = ''): string |
|
20
|
|
|
{ |
|
21
|
|
|
if (empty($url)) { |
|
22
|
|
|
return ''; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$imageUrl = ''; |
|
26
|
|
|
|
|
27
|
|
|
$format = static::detectFormat($format); |
|
28
|
|
|
if ($format === 'webp') { |
|
29
|
|
|
$useProgressive = false; |
|
30
|
|
|
} |
|
31
|
|
|
if ($useProgressive === null) { |
|
32
|
|
|
$useProgressive = ! empty(config('contentful.image.use_progressive')); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (empty($fit)) { |
|
36
|
|
|
$fit = 'fill'; |
|
37
|
|
|
} else { |
|
38
|
|
|
$fit = ($fit !== 'default') ? $fit : null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
collect([ |
|
42
|
|
|
'w' => $width, |
|
43
|
|
|
'h' => $height, |
|
44
|
|
|
'q' => ! empty($quality) ? $quality : config('contentful.image.default_quality'), |
|
45
|
|
|
'fm' => config('contentful.image.use_webp') ? $format : null, |
|
46
|
|
|
'fl' => $useProgressive ? 'progressive' : null, |
|
47
|
|
|
'fit' => $fit, |
|
48
|
|
|
])->filter(function ($value) { |
|
49
|
|
|
return ! empty($value); |
|
50
|
|
|
})->each(function ($value, $key) use (& $imageUrl) { |
|
51
|
|
|
$imageUrl .= $key . '=' . $value . '&'; |
|
52
|
|
|
}); |
|
53
|
|
|
|
|
54
|
|
|
if (str_contains($url, '?')) { |
|
55
|
|
|
$url = explode('?', $url); |
|
56
|
|
|
$url = $url[0]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$searchHosts = config('contentful.image.search_hosts'); |
|
60
|
|
|
$replaceHost = config('contentful.image.replace_host'); |
|
61
|
|
|
if (! empty($searchHosts) and ! empty($replaceHost)) { |
|
|
|
|
|
|
62
|
|
|
$url = str_replace(explode(',', $searchHosts), $replaceHost, $url); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return ! empty($url) ? $url . '?' . trim($imageUrl, '&'): ''; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Auto-detect image format to serve (based on browser capability). |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $format |
|
72
|
|
|
* @return string|null |
|
73
|
|
|
*/ |
|
74
|
|
|
protected static function detectFormat(string $format = ''): ?string |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var \Jenssegers\Agent\Agent $agent */ |
|
77
|
|
|
$agent = app('agent'); |
|
78
|
|
|
|
|
79
|
|
|
if (empty($format)) { |
|
80
|
|
|
$browser = mb_strtolower($agent->browser()); |
|
81
|
|
|
if (($browser === 'chrome') and ! $agent->isMobile()) { |
|
|
|
|
|
|
82
|
|
|
$format = config('contentful.image.use_webp') ? 'webp' : null; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $format; |
|
87
|
|
|
} |
|
88
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.