From 0e34e43abbe219d4bff336e7529a6162f512867a Mon Sep 17 00:00:00 2001 From: Serkan Date: Tue, 6 Apr 2021 08:03:15 +0300 Subject: [PATCH] The return early pattern applied to improve readability. (#8894) --- app/Helpers/Helper.php | 60 +++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index a237775428..fc6c23228e 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -1003,38 +1003,38 @@ class Helper * @return string path to uploaded image or false if something went wrong */ public static function processUploadedImage(String $image_data, String $save_path) { - if ($image_data != null && $save_path != null) { - // After modification, the image is prefixed by mime info like the following: - // data:image/jpeg;base64,; This causes the image library to be unhappy, so we need to remove it. - $header = explode(';', $image_data, 2)[0]; - // Grab the image type from the header while we're at it. - $extension = substr($header, strpos($header, '/')+1); - // Start reading the image after the first comma, postceding the base64. - $image = substr($image_data, strpos($image_data, ',')+1); - - $file_name = str_random(25).".".$extension; - - $directory= public_path($save_path); - // Check if the uploads directory exists. If not, try to create it. - if (!file_exists($directory)) { - mkdir($directory, 0755, true); - } - - $path = public_path($save_path.$file_name); - - try { - Image::make($image)->resize(500, 500, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - })->save($path); - } catch (\Exception $e) { - return false; - } - - return $file_name; + if ($image_data == null || $save_path == null) { + return false; } - return false; + // After modification, the image is prefixed by mime info like the following: + // data:image/jpeg;base64,; This causes the image library to be unhappy, so we need to remove it. + $header = explode(';', $image_data, 2)[0]; + // Grab the image type from the header while we're at it. + $extension = substr($header, strpos($header, '/')+1); + // Start reading the image after the first comma, postceding the base64. + $image = substr($image_data, strpos($image_data, ',')+1); + + $file_name = str_random(25).".".$extension; + + $directory= public_path($save_path); + // Check if the uploads directory exists. If not, try to create it. + if (!file_exists($directory)) { + mkdir($directory, 0755, true); + } + + $path = public_path($save_path.$file_name); + + try { + Image::make($image)->resize(500, 500, function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + })->save($path); + } catch (\Exception $e) { + return false; + } + + return $file_name; } }