From d18b37adcfe407757bce7be228d1ef6890305573 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 23 Jan 2026 17:53:22 +0100 Subject: [PATCH 1/2] Add width / height in upload formatter --- .../Fields/Formatters/UploadFormatter.php | 31 ++++++++++++++++++- .../Fields/Formatters/UploadFormatterTest.php | 4 ++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Form/Fields/Formatters/UploadFormatter.php b/src/Form/Fields/Formatters/UploadFormatter.php index ec134d3a3..19562363f 100644 --- a/src/Form/Fields/Formatters/UploadFormatter.php +++ b/src/Form/Fields/Formatters/UploadFormatter.php @@ -6,6 +6,7 @@ use Code16\Sharp\Form\Fields\SharpFormUploadField; use Code16\Sharp\Utils\FileUtil; use Code16\Sharp\Utils\Uploads\SharpUploadManager; +use Illuminate\Filesystem\LocalFilesystemAdapter; use Illuminate\Support\Facades\Storage; class UploadFormatter extends SharpFieldFormatter implements FormatsAfterUpdate @@ -50,7 +51,7 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar 'filters' => $field->isImageTransformOriginal() ? null : $value['filters'] ?? null, - ]), function ($formatted) use ($field, $value) { + ]), function (&$formatted) use ($field, $value, $uploadedFieldRelativePath) { if ($field->storageDisk()) { app(SharpUploadManager::class)->queueHandleUploadedFile( uploadedFileName: $value['name'], @@ -63,6 +64,11 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar : null, ); } + + if ($size = $this->getImageSize($uploadedFieldRelativePath, $formatted['mime_type'])) { + $formatted['width'] = $size['width']; + $formatted['height'] = $size['height']; + } }); } @@ -108,4 +114,27 @@ protected function normalizeFromFront(?array $value, ?array $formatted = null): 'filters' => $formatted['filters'] ?? $value['filters'] ?? null, ])->whereNotNull()->toArray(); } + + protected function getImageSize(string $filePath, string $mimeType): ?array + { + // image size only available if tmp is stored locally + if (! Storage::disk(sharp()->config()->get('uploads.tmp_disk')) instanceof LocalFilesystemAdapter) { + return null; + } + + if (! str_starts_with($mimeType, 'image/')) { + return null; + } + + $realPath = Storage::disk(sharp()->config()->get('uploads.tmp_disk'))->path($filePath); + + if ($size = @getimagesize($realPath)) { + return [ + 'width' => $size[0], + 'height' => $size[1], + ]; + } + + return null; + } } diff --git a/tests/Unit/Form/Fields/Formatters/UploadFormatterTest.php b/tests/Unit/Form/Fields/Formatters/UploadFormatterTest.php index e2bc59cac..c8fc0c282 100644 --- a/tests/Unit/Form/Fields/Formatters/UploadFormatterTest.php +++ b/tests/Unit/Form/Fields/Formatters/UploadFormatterTest.php @@ -127,7 +127,7 @@ $formatter = app(UploadFormatter::class); UploadedFile::fake() - ->image('image.jpg') + ->image('image.jpg', width: 10, height: 10) ->storeAs('/tmp', 'image.jpg', ['disk' => 'local']); $field = SharpFormUploadField::make('upload')->setStorageTemporary(); @@ -144,5 +144,7 @@ 'disk' => 'local', 'mime_type' => 'image/jpeg', 'size' => 695, + 'width' => 10, + 'height' => 10, ]); }); From a291469acfbf14a081a36edd2351074b1c6f2590 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 23 Jan 2026 18:13:13 +0100 Subject: [PATCH 2/2] rename for clarity --- src/Form/Fields/Formatters/UploadFormatter.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Form/Fields/Formatters/UploadFormatter.php b/src/Form/Fields/Formatters/UploadFormatter.php index 19562363f..ff6456a79 100644 --- a/src/Form/Fields/Formatters/UploadFormatter.php +++ b/src/Form/Fields/Formatters/UploadFormatter.php @@ -65,9 +65,9 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar ); } - if ($size = $this->getImageSize($uploadedFieldRelativePath, $formatted['mime_type'])) { - $formatted['width'] = $size['width']; - $formatted['height'] = $size['height']; + if ($dimensions = $this->getImageDimensions($uploadedFieldRelativePath, $formatted['mime_type'])) { + $formatted['width'] = $dimensions['width']; + $formatted['height'] = $dimensions['height']; } }); } @@ -115,7 +115,7 @@ protected function normalizeFromFront(?array $value, ?array $formatted = null): ])->whereNotNull()->toArray(); } - protected function getImageSize(string $filePath, string $mimeType): ?array + protected function getImageDimensions(string $tmpFilePath, string $mimeType): ?array { // image size only available if tmp is stored locally if (! Storage::disk(sharp()->config()->get('uploads.tmp_disk')) instanceof LocalFilesystemAdapter) { @@ -126,7 +126,7 @@ protected function getImageSize(string $filePath, string $mimeType): ?array return null; } - $realPath = Storage::disk(sharp()->config()->get('uploads.tmp_disk'))->path($filePath); + $realPath = Storage::disk(sharp()->config()->get('uploads.tmp_disk'))->path($tmpFilePath); if ($size = @getimagesize($realPath)) { return [