Skip to content

Commit 197acab

Browse files
authored
Merge pull request #7 from smmehdisharifi/feature/configurable-structure-for-flatten-images
configurable structure for flatten images
2 parents 9eefa21 + 49a0bf0 commit 197acab

File tree

5 files changed

+61
-37
lines changed

5 files changed

+61
-37
lines changed

config/file-storage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'jfif' => 'jpg',
2020
'heic' => 'jpg',
2121
],
22+
'flatten' => true,
2223
],
2324
DownscaleModifier::class => [
2425
'width' => 2000,
@@ -28,6 +29,7 @@
2829
'width' => 150,
2930
'height' => 150,
3031
'format' => 'jpg',
32+
'flatten' => true,
3133
],
3234
],
3335
];

src/Modifiers/DownscaleModifier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function handle(Media $media): Media
3131
targetFile: $media->original_relative_path,
3232
width: $this->options['width'],
3333
height: $this->options['height'],
34+
flatten: false,
3435
);
3536

3637
return $media;

src/Modifiers/FormatModifier.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ public function __construct(
1919

2020
public function canHandle(Media $media): bool
2121
{
22-
$formats = array_keys($this->options['formats']);
23-
$ext = pathinfo($media->file_name, PATHINFO_EXTENSION);
22+
if (! $media->is_image) {
23+
return false;
24+
}
25+
26+
[$sourceExt, $targetExt, $flatten] = $this->resolveOptions($media);
2427

25-
return $media->is_image && in_array($ext, $formats, true);
28+
return $targetExt !== $sourceExt || $flatten;
2629
}
2730

2831
/**
@@ -33,23 +36,35 @@ public function canHandle(Media $media): bool
3336
*/
3437
public function handle(Media $media): Media
3538
{
39+
[, $targetExt, $flatten] = $this->resolveOptions($media);
3640
$originalFile = $media->original_relative_path;
37-
$ext = pathinfo($media->file_name, PATHINFO_EXTENSION);
38-
$newName = pathinfo($originalFile, PATHINFO_FILENAME).'.'.$this->options['formats'][$ext];
41+
$newName = pathinfo($originalFile, PATHINFO_FILENAME).'.'.$targetExt;
3942

4043
$this->image->convert(
4144
disk: $media->disk,
4245
sourceFile: $originalFile,
4346
targetFile: $media->dir_relative_path.$newName,
47+
flatten: $flatten,
4448
);
4549

4650
$media->file_name = $newName;
4751
if (! $media->save()) {
4852
throw new PersistenceFailedException;
4953
}
5054

51-
$this->filesystem->deleteFile($media->disk, $originalFile);
55+
if ($media->wasChanged('file_name')) {
56+
$this->filesystem->deleteFile($media->disk, $originalFile);
57+
}
5258

5359
return $media;
5460
}
61+
62+
private function resolveOptions(Media $media): array
63+
{
64+
$sourceExt = pathinfo($media->file_name, PATHINFO_EXTENSION);
65+
$targetExt = $this->options['formats'][$sourceExt] ?? $sourceExt;
66+
$flatten = $this->options['flatten'] ?? false;
67+
68+
return [$sourceExt, $targetExt, $flatten];
69+
}
5570
}

src/Modifiers/PreviewModifier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function handle(Media $media): Media
3838
targetFile: $media->dir_relative_path.$media->preview,
3939
width: $this->options['width'],
4040
height: $this->options['height'],
41+
flatten: $this->options['flatten'] ?? false,
4142
);
4243

4344
if (! $media->save()) {

src/Utility/Image.php

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,65 @@
1010
class Image
1111
{
1212
/**
13+
* Resize the image and save it.
14+
*
1315
* @throws \Imahmood\FileStorage\Exceptions\NotWritableException
1416
* @throws \Jcupitt\Vips\Exception
1517
*/
16-
public function resize(string $disk, string $sourceFile, string $targetFile, int $width, int $height): void
18+
public function resize(string $disk, string $sourceFile, string $targetFile, int $width, int $height, bool $flatten): void
1719
{
18-
$image = VipsImage::newFromBuffer(
19-
Storage::disk($disk)->get($sourceFile)
20-
);
21-
22-
if ($image->hasAlpha()) {
23-
$image = $image->flatten(['background' => [255, 255, 255]]);
24-
}
20+
$image = $this->loadImage($disk, $sourceFile);
2521

2622
$width = min($image->width, $width);
2723
$height = min($image->height, $height);
28-
$ext = pathinfo($targetFile, PATHINFO_EXTENSION);
29-
30-
$saved = Storage::disk($disk)->put(
31-
path: $targetFile,
32-
contents: $image->thumbnail_image($width, ['height' => $height])->writeToBuffer('.'.$ext),
33-
);
34-
35-
$image = null;
3624

37-
if ($saved === false) {
38-
throw new NotWritableException("Can't write image data to path `$targetFile`");
25+
if ($flatten && $image->hasAlpha()) {
26+
$image = $image->flatten(['background' => [255, 255, 255]]);
3927
}
28+
29+
$image = $image->thumbnail_image($width, ['height' => $height]);
30+
$this->saveImage($disk, $targetFile, $image);
4031
}
4132

4233
/**
34+
* Convert the image format and save it.
35+
*
4336
* @throws \Imahmood\FileStorage\Exceptions\NotWritableException
4437
* @throws \Jcupitt\Vips\Exception
4538
*/
46-
public function convert(string $disk, string $sourceFile, string $targetFile): void
39+
public function convert(string $disk, string $sourceFile, string $targetFile, bool $flatten): void
4740
{
48-
$image = VipsImage::newFromBuffer(
49-
Storage::disk($disk)->get($sourceFile)
50-
);
41+
$image = $this->loadImage($disk, $sourceFile);
5142

52-
if ($image->hasAlpha()) {
43+
if ($flatten && $image->hasAlpha()) {
5344
$image = $image->flatten(['background' => [255, 255, 255]]);
5445
}
5546

56-
$ext = pathinfo($targetFile, PATHINFO_EXTENSION);
47+
$this->saveImage($disk, $targetFile, $image);
48+
}
5749

58-
$saved = Storage::disk($disk)->put(
59-
path: $targetFile,
60-
contents: $image->writeToBuffer('.'.$ext),
61-
);
50+
/**
51+
* Load image from storage.
52+
*/
53+
private function loadImage(string $disk, string $path): VipsImage
54+
{
55+
$content = Storage::disk($disk)->get($path);
56+
57+
return VipsImage::newFromBuffer($content);
58+
}
6259

63-
$image = null;
60+
/**
61+
* Save image to storage.
62+
*
63+
* @throws \Imahmood\FileStorage\Exceptions\NotWritableException
64+
*/
65+
private function saveImage(string $disk, string $path, VipsImage $image): void
66+
{
67+
$extension = '.'.pathinfo($path, PATHINFO_EXTENSION);
68+
$buffer = $image->writeToBuffer($extension);
6469

65-
if ($saved === false) {
66-
throw new NotWritableException("Can't write image data to path `$targetFile`");
70+
if (! Storage::disk($disk)->put($path, $buffer)) {
71+
throw new NotWritableException("Can't write image data to path `$path`");
6772
}
6873
}
6974
}

0 commit comments

Comments
 (0)