PHP Web-based Image Resizer Function in PHP

Resize and compress images with a web-based PHP function. Easily adjust dimensions and choose between cropping or maintaining aspect ratio. Improve website performance with faster loading images.

<?php

function resize_image($file, $w, $h, $crop = false)
{
    $file = file_get_contents($file);

    $src = imagecreatefromstring($file);
    if (!$src) {
        return false;
    }

    $width = imagesx($src);
    $height = imagesy($src);

    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width - ($width * abs($r - $w / $h)));
        } else {
            $height = ceil($height - ($height * abs($r - $w / $h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w / $h > $r) {
            $newwidth = $h * $r;
            $newheight = $h;
        } else {
            $newheight = $w / $r;
            $newwidth = $w;
        }
    }

    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    ob_start();
    imagepng($dst);
    $data = ob_get_contents();
    ob_end_clean();

    return 'data:image/jpeg;base64,' . base64_encode($data);
}

?>
<img src="<?php echo resize_image('img.png', 300, 300); ?>" alt="Faster Review" >

<img src="img.png" alt="" srcset="">