From c77219f1354503938b3c31ed3f09f39afcf85186 Mon Sep 17 00:00:00 2001 From: Lap Dam Date: Tue, 28 Apr 2020 14:42:58 +0700 Subject: [PATCH] feature: base64 upload --- src/Controllers/APIController.php | 45 +++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Controllers/APIController.php b/src/Controllers/APIController.php index 9901fa4..4433489 100644 --- a/src/Controllers/APIController.php +++ b/src/Controllers/APIController.php @@ -147,10 +147,11 @@ public function destroyBulk($entity, Request $request) public function upload(Request $request) { $files = $request->file('file'); + $fileBase64 = $request->input('fileBase64'); $directoryPath = env('APIFY_UPLOAD_PATH', '/home/upload'); $customDirectoryPath = $request->get('customDirectoryPath'); $ruleValidate = $request->get('ruleValidate', []); - if (empty($files)) { + if (empty($files) && empty($fileBase64)) { $result = ["result" => "File required!"]; return $this->error($result); } @@ -161,9 +162,23 @@ public function upload(Request $request) mkdir($directoryPath, 0777, true); } } + if (!empty($fileBase64)) { + if (is_array($fileBase64)) { + $output = []; + foreach($fileBase64 as $file) { + $path = $this->uploadBase64($file, $customDirectoryPath, $directoryPath); + if (!empty($path)) { + array_push($output, $path); + } + $result = ['result' => $output]; + } + }else{ + $path = $this->uploadBase64($fileBase64, $customDirectoryPath, $directoryPath); + $result = ['result' => $path]; + } + }else if (is_array($files)) { //mutiple - if (is_array($files)) { $output = []; foreach($files as $file) { $validate = $this->validateUpload($ruleValidate, $file); @@ -242,4 +257,30 @@ private function validateUpload($rule, $file) { } return ['status' => true]; } + + private function uploadBase64($file,$customDirectoryPath,$directoryPath){ + $retVal = ''; + $file = preg_replace('/^data:image\/\w+;base64,/', '', $file); + $file = str_replace(' ', '+', $file); + $image = base64_decode($file); + if (imagecreatefromstring(base64_decode($file)) !== false ) { + $f = finfo_open(); + $type = finfo_buffer($f, $image, FILEINFO_MIME_TYPE); + + if ($type) { + $type = explode('/', $type)[1]; + } + $newFileName = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(10/strlen($x)) )),1,10) . time() . '.' . $type; + $fullRelativePath = $newFileName; + if ($customDirectoryPath) { + $fullRelativePath = "/" . $customDirectoryPath . '/' . $newFileName; + } + $isSuccess = file_put_contents($directoryPath.$fullRelativePath, $image); + if ($isSuccess) { + $retVal = $fullRelativePath; + } + } + + return $retVal; + } }