validate($request, $identification->rules()); $dbIdentification = Identification::where('id_user', $request->id_user)->first(); if($dbIdentification) return $this->errorResponse(trans('errors.identification_carried_out')); if($request->has('id_user')) $user = User::findOrFail($request->id_user); elseif ($request->has('phone_number')) $user = User::where('phone',$request->phone_number)->firstOrFail(); $identification->fill($request->all()); $identification->id_user = $user->id; $identification->status = 0; $identification->save(); $this->sendMail($user->email,trans('messages.successful_identification'), trans('messages.successful_identification_message',['name'=>$identification->lastname.' '.$identification->firstname])); return $this->successResponse(trans('messages.successful_identification',['name'=>$identification->lastname.' '.$identification->firstname])); } public function validateIdentification(Request $request, $id_identification) { $identification = Identification::findOrFail($id_identification); if ($identification->status == 1) { return $this->errorResponse(trans('messages.identification_already_validated')); } // dd($request->allFiles()); $this->validate($request, [ 'id_network' => 'required|integer|min:0|not_in:0', 'id_country' => 'required|integer|min:0|not_in:0', 'document_image_front' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6048', 'document_image_back' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6048', 'user_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6048', ]); $identification->idNetwork = $request->id_network; $identification->id_country = $request->id_country; $identification->document_image_front = $this->uploadImage($request,'document_image_front','D-F',"documents"); $identification->document_image_back = $this->uploadImage($request,'document_image_back','D-B',"documents"); $identification->user_image = $this->uploadImage($request,'user_image','U',"photos"); $identification->status = 1; $identification->save(); $this->sendMail($identification->user->email,trans('messages.validated_identification'),trans('messages.validated_identification_message')); return $this->successResponse(trans('messages.validated_identification')); } public function fetchIdentification($user_phone) { // return Identification::where('id_user', $id_user)->firstOrFail(); $user = User::where('phone', $user_phone)->first(); if($user){ $identification = Identification::where('id_user', $user->id)->first(); $data['isIdentified'] = false; $data['isIdentifiedValidated'] = false; $data['data'] = $identification; if($identification){ $data['isIdentified'] = true; $data['isIdentifiedValidated'] = $identification->status == 1 ; } return $this->successResponse($data); }else{ return $this->errorResponse(trans('errors.user_phone_not_exist'),Response::HTTP_NOT_FOUND); } } public function verifyIdentification($user_phone) { $user = User::where('phone', $user_phone)->first(); if($user){ $identification = Identification::where('id_user', $user->id)->first(); if($identification){ return $this->errorResponse(trans('messages.user_identificated')); }else return $this->successResponse($user); }else{ return $this->errorResponse(trans('errors.user_phone_not_exist'),Response::HTTP_NOT_FOUND); } } private function generateRandomString($length = 10) { $characters = '23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } public function uploadImage(Request $request , $key , $imageCode, $folderName) { // if ($request->hasFile('image')) { $original_filename = $request->file($key)->getClientOriginalName(); $original_filename_arr = explode('.', $original_filename); $file_ext = end($original_filename_arr); $image = $imageCode.'-' . time() . '.' . $file_ext; //Check if the directory already exists. $directoryName = './'.$folderName; if(!is_dir($directoryName)){ //Directory does not exist, so lets create it. mkdir($directoryName, 0755); } // Allow certain file formats // $allowTypes = array('jpg', 'png', 'jpeg', 'gif'); // if (in_array(strtolower($file_ext), $allowTypes)) { $compressedImage = $this->compressImage($request->file($key), './'.$folderName.'/' . $image, 70); if ($compressedImage) { return $image; } else { return $this->errorResponse(trans('errors.compression_failed')); } // } else { // return $this->errorResponse('Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'); // } // } else { // return $this->errorResponse('File not found'); // } } /* * Custom function to compress image size and * upload to the server using PHP */ private function compressImage($source, $destination, $quality) { // Get image info $imgInfo = getimagesize($source); $mime = $imgInfo['mime']; // Create a new image from file switch ($mime) { case 'image/jpeg': $image = imagecreatefromjpeg($source); break; case 'image/png': $image = imagecreatefrompng($source); break; case 'image/gif': $image = imagecreatefromgif($source); break; default: $image = imagecreatefromjpeg($source); } // Save image imagejpeg($image, $destination, $quality); // Return compressed image return $destination; } }