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
|
|
|
$imageGenerator = $this->determineImageGenerator($media); |
49
|
|
|
|
50
|
|
|
if (! $imageGenerator || $conversions->isEmpty()) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$temporaryDirectory = new TemporaryDirectory(storage_path('medialibrary/temp')); |
55
|
|
|
|
56
|
|
|
$copiedOriginalFile = app(Filesystem::class)->copyFromMediaLibrary( |
57
|
|
|
$media, |
58
|
|
|
$temporaryDirectory->path(str_random(16).'.'.$media->extension) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
foreach ($conversions as $conversion) { |
62
|
|
|
$copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion); |
63
|
|
|
|
64
|
|
|
$conversionResult = $this->performConversion($media, $conversion, $copiedOriginalFile); |
65
|
|
|
|
66
|
|
|
$newFileName = $conversion->getName() |
67
|
|
|
.'.' |
68
|
|
|
.$conversion->getResultExtension(pathinfo($copiedOriginalFile, PATHINFO_EXTENSION)); |
69
|
|
|
|
70
|
|
|
$renamedFile = MediaLibraryFileHelper::renameInDirectory($conversionResult, $newFileName); |
71
|
|
|
|
72
|
|
|
app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, true); |
73
|
|
|
|
74
|
|
|
event(new ConversionHasBeenCompleted($media, $conversion)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$temporaryDirectory->delete(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function performConversion(Media $media, Conversion $conversion, string $imageFile): string |
81
|
|
|
{ |
82
|
|
|
$conversionTempFile = pathinfo($imageFile, PATHINFO_DIRNAME).'/'.str_random(16) |
83
|
|
|
.$conversion->getName() |
84
|
|
|
.'.' |
85
|
|
|
.$media->extension; |
86
|
|
|
|
87
|
|
|
File::copy($imageFile, $conversionTempFile); |
88
|
|
|
|
89
|
|
|
Image::load($conversionTempFile) |
90
|
|
|
->useImageDriver(config('medialibrary.image_driver')) |
91
|
|
|
->manipulate($conversion->getManipulations()) |
92
|
|
|
->save(); |
93
|
|
|
|
94
|
|
|
return $conversionTempFile; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function dispatchQueuedConversions(Media $media, ConversionCollection $queuedConversions) |
98
|
|
|
{ |
99
|
|
|
$job = new PerformConversions($queuedConversions, $media); |
100
|
|
|
|
101
|
|
|
if ($customQueue = config('medialibrary.queue_name')) { |
102
|
|
|
$job->onQueue($customQueue); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
app(Dispatcher::class)->dispatch($job); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
110
|
|
|
* |
111
|
|
|
* @return \Spatie\MediaLibrary\ImageGenerators\ImageGenerator|null |
112
|
|
|
*/ |
113
|
|
|
public function determineImageGenerator(Media $media) |
114
|
|
|
{ |
115
|
|
|
return $media->getImageGenerators() |
116
|
|
|
->map(function (string $imageGeneratorClassName) { |
117
|
|
|
return app($imageGeneratorClassName); |
118
|
|
|
}) |
119
|
|
|
->first(function (ImageGenerator $imageGenerator) use ($media) { |
120
|
|
|
return $imageGenerator->canConvert($media); |
121
|
|
|
}); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|