Skip to content

Commit d18b37a

Browse files
committed
Add width / height in upload formatter
1 parent 0a730b8 commit d18b37a

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/Form/Fields/Formatters/UploadFormatter.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Code16\Sharp\Form\Fields\SharpFormUploadField;
77
use Code16\Sharp\Utils\FileUtil;
88
use Code16\Sharp\Utils\Uploads\SharpUploadManager;
9+
use Illuminate\Filesystem\LocalFilesystemAdapter;
910
use Illuminate\Support\Facades\Storage;
1011

1112
class UploadFormatter extends SharpFieldFormatter implements FormatsAfterUpdate
@@ -50,7 +51,7 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar
5051
'filters' => $field->isImageTransformOriginal()
5152
? null
5253
: $value['filters'] ?? null,
53-
]), function ($formatted) use ($field, $value) {
54+
]), function (&$formatted) use ($field, $value, $uploadedFieldRelativePath) {
5455
if ($field->storageDisk()) {
5556
app(SharpUploadManager::class)->queueHandleUploadedFile(
5657
uploadedFileName: $value['name'],
@@ -63,6 +64,11 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar
6364
: null,
6465
);
6566
}
67+
68+
if ($size = $this->getImageSize($uploadedFieldRelativePath, $formatted['mime_type'])) {
69+
$formatted['width'] = $size['width'];
70+
$formatted['height'] = $size['height'];
71+
}
6672
});
6773
}
6874

@@ -108,4 +114,27 @@ protected function normalizeFromFront(?array $value, ?array $formatted = null):
108114
'filters' => $formatted['filters'] ?? $value['filters'] ?? null,
109115
])->whereNotNull()->toArray();
110116
}
117+
118+
protected function getImageSize(string $filePath, string $mimeType): ?array
119+
{
120+
// image size only available if tmp is stored locally
121+
if (! Storage::disk(sharp()->config()->get('uploads.tmp_disk')) instanceof LocalFilesystemAdapter) {
122+
return null;
123+
}
124+
125+
if (! str_starts_with($mimeType, 'image/')) {
126+
return null;
127+
}
128+
129+
$realPath = Storage::disk(sharp()->config()->get('uploads.tmp_disk'))->path($filePath);
130+
131+
if ($size = @getimagesize($realPath)) {
132+
return [
133+
'width' => $size[0],
134+
'height' => $size[1],
135+
];
136+
}
137+
138+
return null;
139+
}
111140
}

tests/Unit/Form/Fields/Formatters/UploadFormatterTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
$formatter = app(UploadFormatter::class);
128128

129129
UploadedFile::fake()
130-
->image('image.jpg')
130+
->image('image.jpg', width: 10, height: 10)
131131
->storeAs('/tmp', 'image.jpg', ['disk' => 'local']);
132132

133133
$field = SharpFormUploadField::make('upload')->setStorageTemporary();
@@ -144,5 +144,7 @@
144144
'disk' => 'local',
145145
'mime_type' => 'image/jpeg',
146146
'size' => 695,
147+
'width' => 10,
148+
'height' => 10,
147149
]);
148150
});

0 commit comments

Comments
 (0)