1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Helpers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\File; |
6
|
|
|
use Illuminate\Http\UploadedFile; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
class ImageHelpers |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Store the uploaded photos in the Spatie Media Library |
14
|
|
|
* |
15
|
|
|
* @param object $model |
16
|
|
|
* @param object $request |
17
|
19 |
|
* @param string $collectionName |
18
|
|
|
* |
19
|
19 |
|
* @return void |
20
|
19 |
|
*/ |
21
|
|
|
public static function storeImages(object $model, object $request, string $collectionName): void |
22
|
|
|
{ |
23
|
|
|
if (!$request->file($collectionName)) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (is_array($request->file($collectionName))) { // Store multiple images |
28
|
|
|
foreach ($request->file($collectionName) as $file) { |
29
|
|
|
if ($file->isValid()) { |
30
|
|
|
$model->addMedia($file)->toMediaCollection($collectionName); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} else { // Store single image |
34
|
|
|
$file = $request->file($collectionName); |
35
|
|
|
if ($file->isValid()) { |
36
|
|
|
$model->addMedia($file)->toMediaCollection($collectionName); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Delete photos from the Spatie Media Library |
43
|
|
|
* |
44
|
|
|
* @param object $model |
45
|
|
|
* @param object $request |
46
|
8 |
|
* @param string $collectionName |
47
|
|
|
* |
48
|
8 |
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public static function deleteImages(object $model, object $request, string $collectionName): void |
51
|
|
|
{ |
52
|
|
|
if ($request->introimage_delete == 'true') { |
53
|
|
|
$mediaItems = $model->getMedia($collectionName); |
54
|
8 |
|
if (!is_null($mediaItems[0])) { |
55
|
|
|
$mediaItems[0]->delete(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return an array with the thumb images ulrs |
62
|
|
|
* |
63
|
|
|
* @param object $model |
64
|
|
|
* @param string $collectionName |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public static function getThumbsUrls(object $model, string $collectionName): array |
69
|
|
|
{ |
70
|
|
|
$thumbUrls = []; |
71
|
|
|
|
72
|
|
|
//$model = $this->getById($modelId); |
73
|
|
|
foreach ($model->getMedia($collectionName) as $photo) { |
74
|
|
|
$thumbUrls[] = $photo->getUrl('thumb'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $thumbUrls; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Store an image using Spatie Media Library |
82
|
|
|
* The $photo parameter is an image in Base64 string format. |
83
|
|
|
* |
84
|
|
|
* @param object $model |
85
|
|
|
* @param string|null $photo |
86
|
|
|
* @param string $collectionName |
87
|
|
|
*/ |
88
|
|
|
public static function storeImageFromLivewireComponent(object $model, ?string $photo, string $collectionName): void |
89
|
|
|
{ |
90
|
|
|
if (!$photo) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$file = self::convertBase64ImageToUploadedFile($photo); |
95
|
|
|
$model->addMedia($file)->toMediaCollection($collectionName); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Convert a base64 image to UploadedFile Laravel |
100
|
|
|
* |
101
|
|
|
* @param string $base64File |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Http\UploadedFile |
104
|
|
|
*/ |
105
|
|
|
public static function convertBase64ImageToUploadedFile(string $base64File): UploadedFile |
106
|
|
|
{ |
107
|
|
|
// decode the base64 file |
108
|
|
|
$fileData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64File)); |
109
|
|
|
|
110
|
|
|
// save it to temporary dir first. |
111
|
|
|
$tmpFilePath = sys_get_temp_dir() . '/' . Str::uuid()->toString(); |
112
|
|
|
file_put_contents($tmpFilePath, $fileData); |
113
|
|
|
|
114
|
|
|
// this just to help us get file info. |
115
|
|
|
$tmpFile = new File($tmpFilePath); |
116
|
|
|
|
117
|
|
|
$file = new UploadedFile( |
118
|
|
|
$tmpFile->getPathname(), |
119
|
|
|
$tmpFile->getFilename(), |
120
|
|
|
$tmpFile->getMimeType(), |
121
|
|
|
0, |
122
|
|
|
true // Mark it as test, since the file isn't from real HTTP POST. |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $file; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|