使用PHP \ GD上传使用宽高比调整大小的图像

I've been trying to get PHP/GD function to work with my code, but I really don't understand how to get the codes together in single page.

I've checked many code examples but still don't understand.

How can i get this File input to resize images with aspect ratio saved depending on Height or Width only?

Deleting the original image and keeping the resized one is also a requirement.

<?php
  if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $last_name = $_POST['last_name'];
    $img = $_FILES['img']['name'];
    $imgTmp = $_FILES ['img']['tmp_name'];

    $name = mysqli_real_escape_string($dbc, $name);
    $last_name = mysqli_real_escape_string($dbc, $last_name);
    $img = mysqli_real_escape_string($dbc, $img);
    $imgTmp = mysqli_real_escape_string($dbc, $imgTmp);

    move_uploaded_file($imgTmp, "../images/$img");

    $query = "INSERT INTO database(name, last_name, image) VALUES ('$name','$last_name', '$img')";
?>

<form method="post" action="" enctype="multipart/form-data">
    <div>
        <div>
            <input type="text" name="name" placeholder="Name">
        </div>
        <div>
            <input type="text" name="last_name" placeholder="Last name">
        </div>
    </div>
    <div>
        <input type="file" name="img" value="">
    </div>
    <button name="submit" type="submit">Submit</button>
   </form>