1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
6
|
|
|
use ImagickPixel; |
7
|
|
|
use Spatie\Glide\GlideImage; |
8
|
|
|
use Spatie\MediaLibrary\Conversion\Conversion; |
9
|
|
|
use Spatie\MediaLibrary\Conversion\ConversionCollection; |
10
|
|
|
use Spatie\MediaLibrary\Events\ConversionHasBeenCompleted; |
11
|
|
|
use Spatie\MediaLibrary\Helpers\File as MediaLibraryFileHelper; |
12
|
|
|
use Spatie\MediaLibrary\Jobs\PerformConversions; |
13
|
|
|
use Spatie\PdfToImage\Pdf; |
14
|
|
|
|
15
|
|
|
class FileManipulator |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create all derived files for the given media. |
20
|
|
|
* |
21
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
22
|
|
|
*/ |
23
|
|
|
public function createDerivedFiles(Media $media) |
24
|
|
|
{ |
25
|
|
|
if ($media->type === Media::TYPE_OTHER) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (in_array($media->type, [Media::TYPE_PDF, Media::TYPE_SVG]) && !class_exists('Imagick')) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$profileCollection = ConversionCollection::createForMedia($media); |
34
|
|
|
|
35
|
|
|
$this->performConversions($profileCollection->getNonQueuedConversions($media->collection_name), $media); |
36
|
|
|
|
37
|
|
|
$queuedConversions = $profileCollection->getQueuedConversions($media->collection_name); |
38
|
|
|
|
39
|
|
|
if (count($queuedConversions)) { |
40
|
|
|
$this->dispatchQueuedConversions($media, $queuedConversions); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Perform the given conversions for the given media. |
46
|
|
|
* |
47
|
|
|
* @param \Spatie\MediaLibrary\Conversion\ConversionCollection $conversions |
48
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
49
|
|
|
*/ |
50
|
|
|
public function performConversions(ConversionCollection $conversions, Media $media) |
51
|
|
|
{ |
52
|
|
|
$tempDirectory = $this->createTempDirectory(); |
53
|
|
|
|
54
|
|
|
$copiedOriginalFile = $tempDirectory.'/'.str_random(16).'.'.$media->extension; |
55
|
|
|
|
56
|
|
|
app(Filesystem::class)->copyFromMediaLibrary($media, $copiedOriginalFile); |
57
|
|
|
|
58
|
|
|
if ($media->type == Media::TYPE_PDF) { |
59
|
|
|
$copiedOriginalFile = $this->convertPdfToImage($copiedOriginalFile); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($media->type == Media::TYPE_SVG) { |
63
|
|
|
$copiedOriginalFile = $this->convertSvgToImage($copiedOriginalFile); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($conversions as $conversion) { |
67
|
|
|
$conversionResult = $this->performConversion($media, $conversion, $copiedOriginalFile); |
68
|
|
|
|
69
|
|
|
$renamedFile = MediaLibraryFileHelper::renameInDirectory($conversionResult, $conversion->getName().'.'. |
70
|
|
|
$conversion->getResultExtension(pathinfo($copiedOriginalFile, PATHINFO_EXTENSION))); |
71
|
|
|
|
72
|
|
|
app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, true); |
73
|
|
|
|
74
|
|
|
event(new ConversionHasBeenCompleted($media, $conversion)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
File::deleteDirectory($tempDirectory); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Perform the conversion. |
82
|
|
|
* |
83
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
84
|
|
|
* @param Conversion $conversion |
85
|
|
|
* @param string $copiedOriginalFile |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function performConversion(Media $media, Conversion $conversion, string $copiedOriginalFile) |
90
|
|
|
{ |
91
|
|
|
$conversionTempFile = pathinfo($copiedOriginalFile, PATHINFO_DIRNAME).'/'.string()->random(16). |
92
|
|
|
$conversion->getName().'.'.$media->extension; |
93
|
|
|
|
94
|
|
|
File::copy($copiedOriginalFile, $conversionTempFile); |
95
|
|
|
|
96
|
|
|
foreach ($conversion->getManipulations() as $manipulation) { |
97
|
|
|
GlideImage::create($conversionTempFile) |
98
|
|
|
->modify($manipulation) |
99
|
|
|
->save($conversionTempFile); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $conversionTempFile; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/* |
106
|
|
|
* Create a directory to store some working files. |
107
|
|
|
*/ |
108
|
|
|
public function createTempDirectory() : string |
109
|
|
|
{ |
110
|
|
|
$tempDirectory = storage_path('medialibrary/temp/'.str_random(16)); |
111
|
|
|
|
112
|
|
|
File::makeDirectory($tempDirectory, 493, true); |
113
|
|
|
|
114
|
|
|
return $tempDirectory; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function convertPdfToImage(string $pdfFile) : string |
118
|
|
|
{ |
119
|
|
|
$imageFile = string($pdfFile)->pop('.').'.jpg'; |
120
|
|
|
|
121
|
|
|
(new Pdf($pdfFile))->saveImage($imageFile); |
122
|
|
|
|
123
|
|
|
return $imageFile; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function convertSvgToImage(string $svgFile) : string |
127
|
|
|
{ |
128
|
|
|
$imageFile = string($svgFile)->pop('.').'.png'; |
129
|
|
|
|
130
|
|
|
$image = new \Imagick(); |
131
|
|
|
$image->readImage($svgFile); |
132
|
|
|
$image->setBackgroundColor(new ImagickPixel('none')); |
133
|
|
|
$image->setImageFormat("png32"); |
134
|
|
|
|
135
|
|
|
file_put_contents($imageFile, $image); |
136
|
|
|
|
137
|
|
|
return $imageFile; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/* |
141
|
|
|
* Dispatch the given conversions. |
142
|
|
|
*/ |
143
|
|
|
protected function dispatchQueuedConversions(Media $media, ConversionCollection $queuedConversions) |
144
|
|
|
{ |
145
|
|
|
$job = new PerformConversions($queuedConversions, $media); |
146
|
|
|
|
147
|
|
|
$customQueue = config('laravel-medialibrary.queue_name'); |
148
|
|
|
|
149
|
|
|
if ($customQueue != '') { |
150
|
|
|
$job->onQueue($customQueue); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($job); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|