PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.3
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.3
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / vendor / spatie / image / src / GlideConversion.php

GlideConversion.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.3, at vendor/spatie/image/src/GlideConversion.php

190 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Spatie\Image;
4
5 use Exception;
6 use FilesystemIterator;
7 use League\Glide\Server;
8 use League\Glide\ServerFactory;
9 use Spatie\Image\Exceptions\CouldNotConvert;
10 use Spatie\Image\Exceptions\InvalidTemporaryDirectory;
11
12 final class GlideConversion
13 {
14 private string $imageDriver = 'gd';
15
16 private ?string $conversionResult = null;
17
18 private string $temporaryDirectory;
19
20 public function __construct(private string $inputImage)
21 {
22 $this->temporaryDirectory = sys_get_temp_dir();
23 }
24
25 public static function create(string $inputImage): self
26 {
27 return new self($inputImage);
28 }
29
30 public function setTemporaryDirectory(string $temporaryDirectory): self
31 {
32 if (! is_dir($temporaryDirectory)) {
33 try {
34 mkdir($temporaryDirectory);
35 } catch (Exception) {
36 throw InvalidTemporaryDirectory::temporaryDirectoryNotCreatable($temporaryDirectory);
37 }
38 }
39
40 if (! is_writable($temporaryDirectory)) {
41 throw InvalidTemporaryDirectory::temporaryDirectoryNotWritable($temporaryDirectory);
42 }
43
44 $this->temporaryDirectory = $temporaryDirectory;
45
46 return $this;
47 }
48
49 public function getTemporaryDirectory(): string
50 {
51 return $this->temporaryDirectory;
52 }
53
54 public function useImageDriver(string $imageDriver): self
55 {
56 $this->imageDriver = $imageDriver;
57
58 return $this;
59 }
60
61 public function performManipulations(Manipulations $manipulations): GlideConversion
62 {
63 foreach ($manipulations->getManipulationSequence() as $manipulationGroup) {
64 $inputFile = $this->conversionResult ?? $this->inputImage;
65
66 $watermarkPath = $this->extractWatermarkPath($manipulationGroup);
67
68 $glideServer = $this->createGlideServer($inputFile, $watermarkPath);
69
70 $glideServer->setGroupCacheInFolders(false);
71
72 $manipulatedImage = $this->temporaryDirectory.DIRECTORY_SEPARATOR.$glideServer->makeImage(
73 pathinfo($inputFile, PATHINFO_BASENAME),
74 $this->prepareManipulations($manipulationGroup)
75 );
76
77 if ($this->conversionResult) {
78 unlink($this->conversionResult);
79 }
80
81 $this->conversionResult = $manipulatedImage;
82 }
83
84 return $this;
85 }
86
87 /**
88 * Removes the watermark path from the manipulationGroup and returns it.
89 * This way it can be injected into the Glide server as the `watermarks` path.
90 */
91 private function extractWatermarkPath(&$manipulationGroup)
92 {
93 if (array_key_exists('watermark', $manipulationGroup)) {
94 $watermarkPath = dirname($manipulationGroup['watermark']);
95
96 $manipulationGroup['watermark'] = basename($manipulationGroup['watermark']);
97
98 return $watermarkPath;
99 }
100 }
101
102 private function createGlideServer($inputFile, string $watermarkPath = null): Server
103 {
104 $config = [
105 'source' => dirname($inputFile),
106 'cache' => $this->temporaryDirectory,
107 'driver' => $this->imageDriver,
108 ];
109
110 if ($watermarkPath) {
111 $config['watermarks'] = $watermarkPath;
112 }
113
114 return ServerFactory::create($config);
115 }
116
117 public function save(string $outputFile): void
118 {
119 if ($this->conversionResult === '' || $this->conversionResult === null) {
120 copy($this->inputImage, $outputFile);
121
122 return;
123 }
124
125 $conversionResultDirectory = pathinfo($this->conversionResult, PATHINFO_DIRNAME);
126
127 copy($this->conversionResult, $outputFile);
128
129 unlink($this->conversionResult);
130
131 if ($conversionResultDirectory !== sys_get_temp_dir() && $this->directoryIsEmpty($conversionResultDirectory)) {
132 rmdir($conversionResultDirectory);
133 }
134 }
135
136 private function prepareManipulations(array $manipulationGroup): array
137 {
138 $glideManipulations = [];
139
140 foreach ($manipulationGroup as $name => $argument) {
141 if ($name !== 'optimize') {
142 $glideManipulations[$this->convertToGlideParameter($name)] = $argument;
143 }
144 }
145
146 return $glideManipulations;
147 }
148
149 private function convertToGlideParameter(string $manipulationName): string
150 {
151 return match ($manipulationName) {
152 'width' => 'w',
153 'height' => 'h',
154 'blur' => 'blur',
155 'pixelate' => 'pixel',
156 'crop' => 'fit',
157 'manualCrop' => 'crop',
158 'orientation' => 'or',
159 'flip' => 'flip',
160 'fit' => 'fit',
161 'devicePixelRatio' => 'dpr',
162 'brightness' => 'bri',
163 'contrast' => 'con',
164 'gamma' => 'gam',
165 'sharpen' => 'sharp',
166 'filter' => 'filt',
167 'background' => 'bg',
168 'border' => 'border',
169 'quality' => 'q',
170 'format' => 'fm',
171 'watermark' => 'mark',
172 'watermarkWidth' => 'markw',
173 'watermarkHeight' => 'markh',
174 'watermarkFit' => 'markfit',
175 'watermarkPaddingX' => 'markx',
176 'watermarkPaddingY' => 'marky',
177 'watermarkPosition' => 'markpos',
178 'watermarkOpacity' => 'markalpha',
179 default => throw CouldNotConvert::unknownManipulation($manipulationName)
180 };
181 }
182
183 private function directoryIsEmpty(string $directory): bool
184 {
185 $iterator = new FilesystemIterator($directory);
186
187 return ! $iterator->valid();
188 }
189 }
190