I think all you would have to do is to update line 111 from:
$unique_file_name = $id . '-'. $common_name .'-' .$i.'.'.$file_ext;
to:
$unique_file_name = $id . '-'. $common_name .'-' .($i + $rsImagesCount->TotalRows).'.'.$file_ext;