Services\UserManager::uploadImage PHP Method

uploadImage() public method

Upload profile photo of user
public uploadImage ( File $file, integer $id = null ) : string
$file File Input file
$id integer User ID (if editing)
return string File Path
    public function uploadImage($file, $id = null)
    {
        if (gettype($file) === 'string') {
            return $file;
        }
        $images_path = User::$images_path;
        File::exists(public_path() . '/uploads/') || File::makeDirectory(public_path() . '/uploads/');
        File::exists(public_path() . '/' . $images_path) || File::makeDirectory(public_path() . '/' . $images_path);
        $file_name = $file->getClientOriginalName();
        $file_ext = File::extension($file_name);
        $only_fname = str_replace('.' . $file_ext, '', $file_name);
        // Add random characters to filename
        $file_name = $only_fname . '_' . str_random(8) . '.' . $file_ext;
        $image = Image::make($file->getRealPath());
        if ($id) {
            // If user is being edited, delete old image
            $old_image = Sentry::findUserById($id)->photo;
            File::exists($old_image) && File::delete($old_image);
        }
        $image->fit(128, 128)->save($images_path . $file_name);
        return $images_path . '/' . $file_name;
    }