|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleImageManager\Managers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\UploadedFile; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Spatie\Image\Image; |
|
8
|
|
|
|
|
9
|
|
|
trait CanCreate |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param string|null $fileName |
|
13
|
|
|
* |
|
14
|
|
|
* @return string |
|
15
|
|
|
*/ |
|
16
|
9 |
|
protected function makeFileName(?string $fileName = null): string |
|
17
|
|
|
{ |
|
18
|
9 |
|
if (!$fileName) { |
|
19
|
2 |
|
return $this->prefix . Str::random(30); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
7 |
|
return $this->prefix . $fileName; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
9 |
|
protected function ensureDirectoryExists(string $fileName): void |
|
26
|
|
|
{ |
|
27
|
|
|
// Be sure directories tree created |
|
28
|
9 |
|
$tmpFile = rtrim(dirname($fileName), '/') . '/.tmp-' . Str::uuid(); |
|
29
|
9 |
|
$this->storage()->put($tmpFile, ''); |
|
|
|
|
|
|
30
|
9 |
|
$this->storage()->delete($tmpFile); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
9 |
|
protected function createOriginalFile(UploadedFile $image, string $newFileName, string $newFileExt): void |
|
34
|
|
|
{ |
|
35
|
9 |
|
$path = "{$newFileName}{$newFileExt}"; |
|
36
|
9 |
|
if (!in_array($newFileExt, $this->immutableExtensions)) { |
|
37
|
8 |
|
$builder = Image::load($image->path()); |
|
38
|
8 |
|
if (is_array($this->original) && |
|
39
|
8 |
|
!empty($this->original['methods']) |
|
40
|
|
|
) { |
|
41
|
8 |
|
foreach ($this->original['methods'] as $method => $attrs) { |
|
42
|
8 |
|
call_user_func_array([$builder, $method], $attrs); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
8 |
|
$builder->save($this->storage()->path($path)); |
|
47
|
|
|
} else { |
|
48
|
1 |
|
$this->storage()->put($path, $image->getContent()); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
protected function createFormats(UploadedFile $image, string $newFileName, string $newFileExt): void |
|
53
|
|
|
{ |
|
54
|
9 |
|
if (!in_array($newFileExt, $this->immutableExtensions)) { |
|
55
|
8 |
|
foreach ($this->formats as $format => $configuration) { |
|
56
|
8 |
|
$path = "{$newFileName}-{$format}{$newFileExt}"; |
|
57
|
|
|
|
|
58
|
8 |
|
$builder = Image::load($image->path()); |
|
59
|
8 |
|
if (!empty($configuration['methods']) && is_array($configuration['methods'])) { |
|
60
|
8 |
|
foreach ($configuration['methods'] as $method => $attrs) { |
|
61
|
8 |
|
call_user_func_array([$builder, $method], $attrs); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
8 |
|
$builder->save($this->storage()->path($path)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|