|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\Image\Image; |
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
|
8
|
|
|
use Spatie\MediaLibrary\Conversion\Conversion; |
|
9
|
|
|
use Spatie\MediaLibrary\Filesystem\Filesystem; |
|
10
|
|
|
use Spatie\MediaLibrary\Jobs\PerformConversions; |
|
11
|
|
|
use Spatie\TemporaryDirectory\TemporaryDirectory; |
|
12
|
|
|
use Spatie\MediaLibrary\ImageGenerators\ImageGenerator; |
|
13
|
|
|
use Spatie\MediaLibrary\Conversion\ConversionCollection; |
|
14
|
|
|
use Spatie\MediaLibrary\Events\ConversionHasBeenCompleted; |
|
15
|
|
|
use Spatie\MediaLibrary\Helpers\File as MediaLibraryFileHelper; |
|
16
|
|
|
|
|
17
|
|
|
class FileManipulator |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Create all derived files for the given media. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
|
23
|
|
|
*/ |
|
24
|
|
|
public function createDerivedFiles(Media $media) |
|
25
|
|
|
{ |
|
26
|
|
|
$profileCollection = ConversionCollection::createForMedia($media); |
|
27
|
|
|
|
|
28
|
|
|
$this->performConversions( |
|
29
|
|
|
$profileCollection->getNonQueuedConversions($media->collection_name), |
|
30
|
|
|
$media |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
$queuedConversions = $profileCollection->getQueuedConversions($media->collection_name); |
|
34
|
|
|
|
|
35
|
|
|
if ($queuedConversions->isNotEmpty()) { |
|
36
|
|
|
$this->dispatchQueuedConversions($media, $queuedConversions); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Perform the given conversions for the given media. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Spatie\MediaLibrary\Conversion\ConversionCollection $conversions |
|
44
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
|
45
|
|
|
*/ |
|
46
|
|
|
public function performConversions(ConversionCollection $conversions, Media $media) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($conversions->isEmpty()) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$imageGenerator = $this->determineImageGenerator($media); |
|
53
|
|
|
|
|
54
|
|
|
if (! $imageGenerator) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$temporaryDirectory = new TemporaryDirectory($this->getTemporaryDirectoryPath()); |
|
59
|
|
|
|
|
60
|
|
|
$copiedOriginalFile = app(Filesystem::class)->copyFromMediaLibrary( |
|
61
|
|
|
$media, |
|
62
|
|
|
$temporaryDirectory->path(str_random(16).'.'.$media->extension) |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($conversions as $conversion) { |
|
66
|
|
|
$copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion); |
|
67
|
|
|
|
|
68
|
|
|
$conversionResult = $this->performConversion($media, $conversion, $copiedOriginalFile); |
|
69
|
|
|
|
|
70
|
|
|
$newFileName = $conversion->getName() |
|
71
|
|
|
.'.' |
|
72
|
|
|
.$conversion->getResultExtension(pathinfo($copiedOriginalFile, PATHINFO_EXTENSION)); |
|
73
|
|
|
|
|
74
|
|
|
$renamedFile = MediaLibraryFileHelper::renameInDirectory($conversionResult, $newFileName); |
|
75
|
|
|
|
|
76
|
|
|
app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, true); |
|
77
|
|
|
|
|
78
|
|
|
event(new ConversionHasBeenCompleted($media, $conversion)); |
|
79
|
|
|
|
|
80
|
|
|
unlink($renamedFile); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
unlink($copiedOriginalFile); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function performConversion(Media $media, Conversion $conversion, string $imageFile): string |
|
87
|
|
|
{ |
|
88
|
|
|
$conversionTempFile = pathinfo($imageFile, PATHINFO_DIRNAME).'/'.str_random(16) |
|
89
|
|
|
.$conversion->getName() |
|
90
|
|
|
.'.' |
|
91
|
|
|
.$media->extension; |
|
92
|
|
|
|
|
93
|
|
|
File::copy($imageFile, $conversionTempFile); |
|
94
|
|
|
|
|
95
|
|
|
Image::load($conversionTempFile) |
|
96
|
|
|
->useImageDriver(config('medialibrary.image_driver')) |
|
97
|
|
|
->manipulate($conversion->getManipulations()) |
|
98
|
|
|
->save(); |
|
99
|
|
|
|
|
100
|
|
|
return $conversionTempFile; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function dispatchQueuedConversions(Media $media, ConversionCollection $queuedConversions) |
|
104
|
|
|
{ |
|
105
|
|
|
$job = new PerformConversions($queuedConversions, $media); |
|
106
|
|
|
|
|
107
|
|
|
if ($customQueue = config('medialibrary.queue_name')) { |
|
108
|
|
|
$job->onQueue($customQueue); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
app(Dispatcher::class)->dispatch($job); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function getTemporaryDirectoryPath(): string |
|
118
|
|
|
{ |
|
119
|
|
|
$path = config('medialibrary.temp_file_path'); |
|
120
|
|
|
|
|
121
|
|
|
if ($path !== null) { |
|
122
|
|
|
return $path; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return storage_path('medialibrary/temp'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
|
130
|
|
|
* |
|
131
|
|
|
* @return \Spatie\MediaLibrary\ImageGenerators\ImageGenerator|null |
|
132
|
|
|
*/ |
|
133
|
|
|
public function determineImageGenerator(Media $media) |
|
134
|
|
|
{ |
|
135
|
|
|
return $media->getImageGenerators() |
|
136
|
|
|
->map(function (string $imageGeneratorClassName) { |
|
137
|
|
|
return app($imageGeneratorClassName); |
|
138
|
|
|
}) |
|
139
|
|
|
->first(function (ImageGenerator $imageGenerator) use ($media) { |
|
140
|
|
|
return $imageGenerator->canConvert($media); |
|
141
|
|
|
}); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|