Skip to content

Commit 5594e97

Browse files
attreidpaveljanda
authored andcommitted
Add orig_path to save resized images into different folder than original
1 parent 6cd893c commit 5594e97

3 files changed

Lines changed: 61 additions & 28 deletions

File tree

‎.docs/README.md‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ extensions:
2424
Configure extension:
2525
```neon
2626
imageStorage:
27-
data_path: %wwwDir%/../public/data # Filesystem location
28-
data_dir: data # Relative path
29-
algorithm_file: sha1_file # Algorithm to take image prefix directory from
30-
algorithm_content: sha1 # ...
31-
quality: 85 # Default wuality when cropping
32-
default_transform: fit # Default crop transformation
33-
noimage_identifier: images/noimage/no-image.png # No-image image
34-
friendly_url: false # Create friendly URLs?
27+
data_path: %wwwDir%/data # Filesystem location
28+
data_dir: data # Relative path
29+
orig_path: %wwwDir%/../data # Original images dir (if is null, will be same as data_path)
30+
algorithm_file: sha1_file # Algorithm to take image prefix directory from
31+
algorithm_content: sha1 # ...
32+
quality: 85 # Default wuality when cropping
33+
default_transform: fit # Default crop transformation
34+
noimage_identifier: images/noimage/no-image.png # No-image image
35+
friendly_url: false # Create friendly URLs?
3536
```
3637

3738
## Images

‎src/DI/ImageStorageExtension.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function getConfigSchema(): Schema
1616
return Expect::structure([
1717
'data_path' => Expect::string()->required(),
1818
'data_dir' => Expect::string()->required(),
19+
'orig_path' => Expect::string()->default(null),
1920
'algorithm_file' => Expect::string('sha1_file'),
2021
'algorithm_content' => Expect::string('sha1'),
2122
'quality' => Expect::int(85),
@@ -28,6 +29,7 @@ public function getConfigSchema(): Schema
2829
public function loadConfiguration(): void
2930
{
3031
$builder = $this->getContainerBuilder();
32+
$this->config->orig_path = $this->config->orig_path ?? $this->config->data_path;
3133
$config = (array) $this->config;
3234
$builder->addDefinition($this->prefix('storage'))
3335
->setType(ImageStorage::class)

‎src/ImageStorage.php‎

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class ImageStorage
2323
/** @var string */
2424
private $data_dir;
2525

26+
/** @var string */
27+
private $orig_path;
28+
2629
/** @var callable(string): string */
2730
private $algorithm_file;
2831

@@ -60,6 +63,7 @@ class ImageStorage
6063
public function __construct(
6164
string $data_path,
6265
string $data_dir,
66+
string $orig_path,
6367
callable $algorithm_file,
6468
callable $algorithm_content,
6569
int $quality,
@@ -70,6 +74,7 @@ public function __construct(
7074
{
7175
$this->data_path = $data_path;
7276
$this->data_dir = $data_dir;
77+
$this->orig_path = $orig_path;
7378
$this->algorithm_file = $algorithm_file;
7479
$this->algorithm_content = $algorithm_content;
7580
$this->quality = $quality;
@@ -91,20 +96,39 @@ public function delete($arg, bool $onlyChangedImages = false): void
9196
$dir = implode('/', [$this->data_path, $script->namespace, $script->prefix]);
9297
$origFile = $script->name . '.' . $script->extension;
9398

94-
if (!file_exists($dir)) {
95-
return;
96-
}
99+
if ($this->orig_path === $this->data_path) {
100+
if (!file_exists($dir)) {
101+
return;
102+
}
97103

98-
foreach (new DirectoryIterator($dir) as $file_info) {
99-
if (
100-
!preg_match($pattern, $file_info->getFilename())
101-
|| !(!$onlyChangedImages || $origFile !== $file_info->getFilename()
102-
)
103-
) {
104-
continue;
104+
foreach (new DirectoryIterator($dir) as $file_info) {
105+
if (
106+
!preg_match($pattern, $file_info->getFilename())
107+
|| !(!$onlyChangedImages || $origFile !== $file_info->getFilename()
108+
)
109+
) {
110+
continue;
111+
}
112+
113+
unlink($file_info->getPathname());
105114
}
115+
} else {
116+
$rmdir = function (string $path) use (&$rmdir) {
117+
if (is_file($path)) {
118+
unlink($path);
119+
} elseif (is_dir($path)) {
120+
$scan = glob(rtrim($path, '/') . '/*');
121+
foreach ($scan as $sub_path) {
122+
$rmdir($sub_path);
123+
}
124+
rmdir($path);
125+
}
126+
};
106127

107-
unlink($file_info->getPathname());
128+
if (!$onlyChangedImages){
129+
unlink(implode('/', [$this->orig_path, $script->namespace, $script->prefix, $origFile]));
130+
}
131+
$rmdir($dir);
108132
}
109133
}
110134

@@ -133,7 +157,6 @@ private static function fixName(string $name): string
133157
return Strings::webalize($name, '._');
134158
}
135159

136-
137160
/**
138161
* @param mixed $content
139162
*/
@@ -167,13 +190,21 @@ public function fromIdentifier($args): Image
167190
}
168191

169192
$identifier = $args[0];
193+
$quality = $args[3] ?? $this->quality;
194+
$flag = $args[2] ?? $this->default_transform;
170195

196+
$orig_file = implode('/', [$this->orig_path, $identifier]);
197+
$data_file = implode('/', [$this->data_path, $identifier]);
171198
$isNoImage = false;
172199

173200
if (count($args) === 1) {
174-
if (!file_exists(implode('/', [$this->data_path, $identifier])) || !$identifier) {
201+
if (!file_exists($orig_file) || !$identifier) {
175202
return $this->getNoImage(true);
176203
}
204+
if (!file_exists($data_file)) {
205+
@mkdir(dirname($data_file), $this->mask, true);
206+
@copy($orig_file, $data_file);
207+
}
177208

178209
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier);
179210
}
@@ -190,16 +221,13 @@ public function fromIdentifier($args): Image
190221
$crop = [(int) $matches[4], (int) $matches[5], (int) $matches[6], (int) $matches[7]];
191222
}
192223

193-
$flag = $args[2] ?? $this->default_transform;
194-
$quality = $args[3] ?? $this->quality;
195-
196224
if (!$identifier) {
197225
$isNoImage = false;
198226
[$script, $file] = $this->getNoImage(false);
199227
} else {
200228
$script = ImageNameScript::fromIdentifier($identifier);
201229

202-
$file = implode('/', [$this->data_path, $script->original]);
230+
$file = $orig_file;
203231

204232
if (!file_exists($file)) {
205233
$isNoImage = true;
@@ -213,8 +241,9 @@ public function fromIdentifier($args): Image
213241
$script->setQuality($quality);
214242

215243
$identifier = $script->getIdentifier();
244+
$data_file = implode('/', [$this->data_path, $identifier]);
216245

217-
if (!file_exists(implode('/', [$this->data_path, $identifier]))) {
246+
if (!file_exists($data_file)) {
218247
if (!file_exists($file)) {
219248
return new Image(false, '#', '#', 'Can not find image');
220249
}
@@ -243,8 +272,9 @@ public function fromIdentifier($args): Image
243272

244273
$_image->resize($size[0], $size[1], $flag);
245274

275+
@mkdir(dirname($data_file), $this->mask, true);
246276
$_image->sharpen()->save(
247-
implode('/', [$this->data_path, $identifier]),
277+
$data_file,
248278
$quality
249279
);
250280
}
@@ -306,7 +336,7 @@ public function getNoImage(bool $return_image = false)
306336
private function getSavePath(string $name, string $namespace, string $checksum): array
307337
{
308338
$prefix = substr($checksum, 0, 2);
309-
$dir = implode('/', [$this->data_path, $namespace, $prefix]);
339+
$dir = implode('/', [$this->orig_path, $namespace, $prefix]);
310340

311341
@mkdir($dir, $this->mask, true); // Directory may exist
312342

0 commit comments

Comments
 (0)