Resize an image in Vanilla PHP

Here is a way to resize images in PHP without needing a framework like ImageMagick or GD

The below code is derived from here. With the function resize_image taking into account the original image width and height (ratio) to then follow that ratio in your resized image.

function resize_image($file, $w, $h)
    //gets image height and width to determine ratio cut
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($w / $h > $r) {
        $newwidth = $h * $r;
        $newheight = $h;
    } else {
        $newheight = $w / $r;
        $newwidth = $w;
    //Get the file extension
    $file_type = end(explode(".", $file));
    switch ($file_type) {
        case "png":
            $src = imagecreatefrompng($file);
        case "jpeg":
        case "jpg":
            $src = imagecreatefromjpeg($file);
    //create a blank image and then copies over the new resized image
    $out = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($out, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    return $out;

//image to be resized
$filename = "original.png";
//save new resized image as
$save_as = "resized.png";
// resize the image with 600x600
$imgData = resize_image($filename, 600, 600);
//saves the image
imagepng($imgData, $save_as);

All you need to change is the input, output image name and the resize dimensions.