1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FaithGen\SDK\Helpers; |
4
|
|
|
|
5
|
|
|
use FaithGen\SDK\Models\Image; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
use Illuminate\Support\Facades\URL; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class ImageHelper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Fetches the image url. |
14
|
|
|
* |
15
|
|
|
* @param string $server |
16
|
|
|
* @param string $folder |
17
|
|
|
* @param Image|null $image |
18
|
|
|
* |
19
|
|
|
* @return object |
20
|
|
|
*/ |
21
|
|
|
private static function getImages(string $server, string $folder, ?Image $image): object |
22
|
|
|
{ |
23
|
|
|
if ($image) { |
24
|
|
|
$imageName = $image->name; |
25
|
|
|
} else { |
26
|
|
|
return static::getDefaultImage(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$paths = [ |
30
|
|
|
'local' => $server.'/storage/'.$folder, |
31
|
|
|
'production' => config('faithgen-sdk.faithgen-cloudfront').$folder, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
$path = $paths[App::environment()]; |
35
|
|
|
|
36
|
|
|
return (object) [ |
37
|
|
|
'_50' => $path.'/50-50/'.$imageName, |
38
|
|
|
'_100' => $path.'/100-100/'.$imageName, |
39
|
|
|
'original' => $path.'/original/'.$imageName, |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Fetches the system logo for default images. |
45
|
|
|
* |
46
|
|
|
* @return object |
47
|
|
|
*/ |
48
|
|
|
private static function getDefaultImage() |
49
|
|
|
{ |
50
|
|
|
$protocol = 'http://'; |
51
|
|
|
|
52
|
|
|
if (Str::of(URL::current())->startsWith('https')) { |
53
|
|
|
$protocol = 'https://'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return (object) [ |
57
|
|
|
'_50' => $protocol.$_SERVER['HTTP_HOST'].'/images/logo-50.png', |
58
|
|
|
'_100' => $protocol.$_SERVER['HTTP_HOST'].'/images/logo-100.png', |
59
|
|
|
'original' => $protocol.$_SERVER['HTTP_HOST'].'/images/logo-original.png', |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the image object for the given object. |
65
|
|
|
* |
66
|
|
|
* @param string $folder |
67
|
|
|
* @param Image|null $image |
68
|
|
|
* @param string|null $server |
69
|
|
|
* |
70
|
|
|
* @return object |
71
|
|
|
*/ |
72
|
|
|
public static function getImage(string $folder, ?Image $image, ?string $server = null) |
73
|
|
|
{ |
74
|
|
|
if (! $server) { |
75
|
|
|
$server = config('faithgen-sdk.ministries-server'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return static::getImages($server, $folder, $image); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|