66use Code16 \Sharp \Form \Fields \SharpFormUploadField ;
77use Code16 \Sharp \Utils \FileUtil ;
88use Code16 \Sharp \Utils \Uploads \SharpUploadManager ;
9+ use Illuminate \Filesystem \LocalFilesystemAdapter ;
910use Illuminate \Support \Facades \Storage ;
1011
1112class 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}
0 commit comments