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(string $url, int $width = 0, int $height = 0, $format = '', int $quality = 0, ?bool $useProgressive = null, $fit = ''): string |
20
|
|
|
{ |
21
|
18 |
|
if (empty($url)) { |
22
|
2 |
|
return ''; |
23
|
|
|
} |
24
|
|
|
|
25
|
16 |
|
$imageUrl = ''; |
26
|
|
|
|
27
|
16 |
|
$format = static::detectFormat($format); |
28
|
16 |
|
if ($format === 'webp') { |
29
|
4 |
|
$useProgressive = false; |
30
|
|
|
} |
31
|
16 |
|
if ($useProgressive === null) { |
32
|
10 |
|
$useProgressive = ! empty(config('contentful.image.use_progressive')); |
33
|
|
|
} |
34
|
|
|
|
35
|
16 |
|
if (empty($fit)) { |
36
|
14 |
|
$fit = 'fill'; |
37
|
|
|
} else { |
38
|
2 |
|
$fit = ($fit !== 'default') ? $fit : null; |
39
|
|
|
} |
40
|
|
|
|
41
|
16 |
|
collect([ |
42
|
16 |
|
'w' => $width, |
43
|
16 |
|
'h' => $height, |
44
|
16 |
|
'q' => ! empty($quality) ? $quality : config('contentful.image.default_quality'), |
45
|
16 |
|
'fm' => config('contentful.image.use_webp') ? $format : null, |
46
|
16 |
|
'fl' => $useProgressive ? 'progressive' : null, |
47
|
16 |
|
'fit' => $fit, |
48
|
|
|
])->filter(function ($value) { |
49
|
16 |
|
return ! empty($value); |
50
|
|
|
})->each(function ($value, $key) use (& $imageUrl) { |
51
|
16 |
|
$imageUrl .= $key . '=' . $value . '&'; |
52
|
16 |
|
}); |
53
|
|
|
|
54
|
16 |
|
if (str_contains($url, '?')) { |
55
|
6 |
|
$url = explode('?', $url); |
56
|
6 |
|
$url = $url[0]; |
57
|
|
|
} |
58
|
|
|
|
59
|
16 |
|
$searchHosts = config('contentful.image.search_hosts'); |
60
|
16 |
|
$replaceHost = config('contentful.image.replace_host'); |
61
|
16 |
|
if (! empty($searchHosts) and ! empty($replaceHost)) { |
|
|
|
|
62
|
16 |
|
$url = str_replace(explode(',', $searchHosts), $replaceHost, $url); |
63
|
|
|
} |
64
|
|
|
|
65
|
16 |
|
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
|
16 |
|
protected static function detectFormat(string $format = ''): ?string |
75
|
|
|
{ |
76
|
|
|
/** @var \Jenssegers\Agent\Agent $agent */ |
77
|
16 |
|
$agent = app('agent'); |
78
|
|
|
|
79
|
16 |
|
if (empty($format)) { |
80
|
16 |
|
$browser = mb_strtolower($agent->browser()); |
81
|
16 |
|
if (($browser === 'chrome') and ! $agent->isMobile()) { |
|
|
|
|
82
|
16 |
|
$format = config('contentful.image.use_webp') ? 'webp' : null; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
16 |
|
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
die
introduces 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 withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.