Skip to content

Commit 5af2325

Browse files
committed
Image Encoding Before Save
- added method `ResourceInterface::getEncodedImage()` - method `ImagePersister::save()` now uses the `getEncodedImage()` from a resource during saving - fixed unit tests
1 parent 44ecc1c commit 5af2325

10 files changed

Lines changed: 167 additions & 371 deletions

src/Bridge/Nette/DI/ImageStorageExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ public function createFileStorage(string $name, FileStorageConfig $config): Serv
386386
new Reference($this->prefix('filesystem.' . $name)),
387387
new Reference($this->prefix('image_manager')),
388388
new Reference($this->prefix('modifier_facade.' . $name)),
389+
new Reference($this->prefix('config.' . $name)),
389390
])
390391
->setAutowired(false);
391392

@@ -419,7 +420,6 @@ public function createFileStorage(string $name, FileStorageConfig $config): Serv
419420
->setType(ImagePersisterInterface::class)
420421
->setFactory(ImagePersister::class, [
421422
new Reference($this->prefix('filesystem.' . $name)),
422-
new Reference($this->prefix('config.' . $name)),
423423
])
424424
->setAutowired(false);
425425

src/Persistence/ImagePersister.php

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
use League\Flysystem\FilesystemOperator;
99
use League\Flysystem\FilesystemReader;
1010
use League\Flysystem\StorageAttributes;
11-
use SixtyEightPublishers\FileStorage\Config\ConfigInterface;
1211
use SixtyEightPublishers\FileStorage\Exception\FilesystemException;
1312
use SixtyEightPublishers\FileStorage\PathInfoInterface as FilePathInfoInterface;
1413
use SixtyEightPublishers\FileStorage\Resource\ResourceInterface;
15-
use SixtyEightPublishers\ImageStorage\Config\Config;
1614
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
1715
use SixtyEightPublishers\ImageStorage\PathInfoInterface as ImagePathInfoInterface;
1816
use SixtyEightPublishers\ImageStorage\Resource\ResourceInterface as ImageResourceInterface;
@@ -26,7 +24,6 @@ final class ImagePersister implements ImagePersisterInterface
2624
{
2725
public function __construct(
2826
private readonly FilesystemOperator $filesystemOperator,
29-
private readonly ConfigInterface $config,
3027
) {}
3128

3229
public function getFilesystem(): FilesystemOperator
@@ -63,7 +60,7 @@ public function save(ResourceInterface $resource, array $config = []): string
6360
$flushCache = self::FILESYSTEM_PREFIX_SOURCE === $prefix && $this->exists($pathInfo);
6461

6562
try {
66-
$this->filesystemOperator->write($prefix . $path, $this->encodeImage($resource), $config);
63+
$this->filesystemOperator->write($prefix . $path, $resource->getEncodedImage(), $config);
6764

6865
if ($flushCache) {
6966
$this->delete($pathInfo, [
@@ -127,24 +124,6 @@ public function delete(FilePathInfoInterface $pathInfo, array $config = []): voi
127124
$this->deleteFile(self::FILESYSTEM_PREFIX_SOURCE . $pathInfo->getPath(), $config);
128125
}
129126

130-
private function encodeImage(ImageResourceInterface $resource): string
131-
{
132-
if (!$resource->hasBeenModified()) {
133-
$contents = @file_get_contents($resource->getLocalFilename());
134-
135-
if (false !== $contents) {
136-
return $contents;
137-
}
138-
}
139-
140-
$quality = (int) ($resource->getEncodeQuality() ?? $this->config[Config::ENCODE_QUALITY] ?? 90);
141-
$format = $resource->getEncodeFormat() ?? '';
142-
$image = $resource->getSource();
143-
$image = $image->encode($format, $quality);
144-
145-
return $image->getEncoded();
146-
}
147-
148127
/**
149128
* @param array<string, mixed> $config
150129
* @throws FilesystemException

src/Resource/ImageResource.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
use Intervention\Image\Image;
88
use SixtyEightPublishers\FileStorage\PathInfoInterface;
99
use SixtyEightPublishers\ImageStorage\Modifier\Facade\ModifierFacadeInterface;
10+
use function file_get_contents;
1011

1112
class ImageResource implements ResourceInterface
1213
{
1314
private bool $modified = false;
1415

1516
private ?string $encodeFormat = null;
1617

17-
private ?int $encodeQuality = null;
18+
private ?string $encodedImage = null;
1819

1920
public function __construct(
2021
private PathInfoInterface $pathInfo,
2122
private Image $image,
2223
private readonly string $localFilename,
2324
private readonly ModifierFacadeInterface $modifierFacade,
25+
private int $encodeQuality,
2426
) {}
2527

2628
public function getPathInfo(): PathInfoInterface
@@ -59,6 +61,7 @@ public function modifyImage(string|array $modifiers, bool $stripMeta = false): s
5961

6062
if ($modifyResult->modified) {
6163
$resource->modified = $modifyResult->modified;
64+
$resource->encodedImage = null;
6265
}
6366

6467
if (null !== $modifyResult->encodeFormat) {
@@ -84,7 +87,7 @@ public function getFilesize(): ?int
8487
return false !== $filesize ? (int) $filesize : null;
8588
}
8689

87-
public function getEncodeQuality(): ?int
90+
public function getEncodeQuality(): int
8891
{
8992
return $this->encodeQuality;
9093
}
@@ -93,4 +96,25 @@ public function getEncodeFormat(): ?string
9396
{
9497
return $this->encodeFormat;
9598
}
99+
100+
public function getEncodedImage(): string
101+
{
102+
if (null !== $this->encodedImage) {
103+
return $this->encodedImage;
104+
}
105+
106+
if (!$this->hasBeenModified()) {
107+
$contents = @file_get_contents($this->getLocalFilename());
108+
109+
if (false !== $contents) {
110+
return $this->encodedImage = $contents;
111+
}
112+
}
113+
114+
$format = $this->getEncodeFormat() ?? '';
115+
$image = $this->getSource();
116+
$image = $image->encode($format, $this->getEncodeQuality());
117+
118+
return $this->encodedImage = $image->getEncoded();
119+
}
96120
}

src/Resource/ResourceFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
use League\Flysystem\UnableToReadFile;
1313
use Psr\Http\Message\StreamInterface;
1414
use RuntimeException;
15+
use SixtyEightPublishers\FileStorage\Config\ConfigInterface;
1516
use SixtyEightPublishers\FileStorage\Exception\FileNotFoundException;
1617
use SixtyEightPublishers\FileStorage\Exception\FilesystemException;
1718
use SixtyEightPublishers\FileStorage\PathInfoInterface;
1819
use SixtyEightPublishers\FileStorage\Resource\ResourceFactoryInterface;
1920
use SixtyEightPublishers\FileStorage\Resource\ResourceInterface;
21+
use SixtyEightPublishers\ImageStorage\Config\Config;
2022
use SixtyEightPublishers\ImageStorage\Modifier\Facade\ModifierFacadeInterface;
2123
use SixtyEightPublishers\ImageStorage\PathInfoInterface as ImagePathInfoInterface;
2224
use SixtyEightPublishers\ImageStorage\Persistence\ImagePersisterInterface;
@@ -43,6 +45,7 @@ public function __construct(
4345
private readonly FilesystemReader $filesystemReader,
4446
private readonly ImageManager $imageManager,
4547
private readonly ModifierFacadeInterface $modifierFacade,
48+
private readonly ConfigInterface $config,
4649
) {}
4750

4851
/**
@@ -92,6 +95,7 @@ public function createResourceFromFile(PathInfoInterface $pathInfo, string $file
9295
),
9396
localFilename: $filename,
9497
modifierFacade: $this->modifierFacade,
98+
encodeQuality: (int) ($this->config[Config::ENCODE_QUALITY] ?? 90),
9599
);
96100
}
97101

@@ -173,6 +177,7 @@ public function createResourceFromPsrStream(PathInfoInterface $pathInfo, StreamI
173177
),
174178
localFilename: $uri,
175179
modifierFacade: $this->modifierFacade,
180+
encodeQuality: (int) ($this->config[Config::ENCODE_QUALITY] ?? 90),
176181
);
177182
}
178183

@@ -240,6 +245,7 @@ private function createTmpFileResource(PathInfoInterface $pathInfo, string $loca
240245
location: $location,
241246
),
242247
modifierFacade: $this->modifierFacade,
248+
encodeQuality: (int) ($this->config[Config::ENCODE_QUALITY] ?? 90),
243249
tmpFile: new TmpFile($tmpFilename),
244250
);
245251
}

src/Resource/ResourceInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function hasBeenModified(): bool;
2020
*/
2121
public function modifyImage(string|array $modifiers, bool $stripMeta = false): self;
2222

23-
public function getEncodeQuality(): ?int;
23+
public function getEncodeQuality(): int;
2424

2525
public function getEncodeFormat(): ?string;
26+
27+
public function getEncodedImage(): string;
2628
}

src/Resource/TmpFileImageResource.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ public function __construct(
1414
PathInfoInterface $pathInfo,
1515
Image $image,
1616
ModifierFacadeInterface $modifierFacade,
17+
int $encodeQuality,
1718
private readonly TmpFile $tmpFile,
1819
) {
1920
parent::__construct(
2021
pathInfo: $pathInfo,
2122
image: $image,
2223
localFilename: $this->tmpFile->filename,
2324
modifierFacade: $modifierFacade,
25+
encodeQuality: $encodeQuality,
2426
);
2527
}
2628

0 commit comments

Comments
 (0)