These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | |||
81 | $temporaryDirectory->delete(); |
||
82 | } |
||
83 | |||
84 | public function performConversion(Media $media, Conversion $conversion, string $imageFile): string |
||
85 | { |
||
86 | $conversionTempFile = pathinfo($imageFile, PATHINFO_DIRNAME).'/'.str_random(16) |
||
87 | .$conversion->getName() |
||
88 | .'.' |
||
89 | .$media->extension; |
||
90 | |||
91 | File::copy($imageFile, $conversionTempFile); |
||
92 | |||
93 | $supportedFormats = ['jpg', 'pjpg', 'png', 'gif']; |
||
94 | if ($conversion->shouldKeepOriginalImageFormat() && in_array($media->extension, $supportedFormats)) { |
||
95 | $conversion->format($media->extension); |
||
0 ignored issues
–
show
|
|||
96 | } |
||
97 | |||
98 | Image::load($conversionTempFile) |
||
99 | ->useImageDriver(config('medialibrary.image_driver')) |
||
100 | ->manipulate($conversion->getManipulations()) |
||
101 | ->save(); |
||
102 | |||
103 | return $conversionTempFile; |
||
104 | } |
||
105 | |||
106 | protected function dispatchQueuedConversions(Media $media, ConversionCollection $queuedConversions) |
||
107 | { |
||
108 | $job = new PerformConversions($queuedConversions, $media); |
||
109 | |||
110 | if ($customQueue = config('medialibrary.queue_name')) { |
||
111 | $job->onQueue($customQueue); |
||
112 | } |
||
113 | |||
114 | app(Dispatcher::class)->dispatch($job); |
||
115 | } |
||
116 | |||
117 | protected function getTemporaryDirectoryPath(): string |
||
118 | { |
||
119 | $path = is_null(config('medialibrary.temporary_directory_path')) |
||
120 | ? storage_path('medialibrary/temp') |
||
121 | : config('medialibrary.temporary_directory_path'); |
||
122 | |||
123 | return $path.DIRECTORY_SEPARATOR.str_random(32); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param \Spatie\MediaLibrary\Media $media |
||
128 | * |
||
129 | * @return \Spatie\MediaLibrary\ImageGenerators\ImageGenerator|null |
||
130 | */ |
||
131 | public function determineImageGenerator(Media $media) |
||
132 | { |
||
133 | return $media->getImageGenerators() |
||
134 | ->map(function (string $imageGeneratorClassName) { |
||
135 | return app($imageGeneratorClassName); |
||
136 | }) |
||
137 | ->first(function (ImageGenerator $imageGenerator) use ($media) { |
||
138 | return $imageGenerator->canConvert($media); |
||
139 | }); |
||
140 | } |
||
141 | } |
||
142 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.