rodrigobertin
10/31/2017 - 2:24 PM

Rezise php mantain apect radio

<?php

function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $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;
        }
    }

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

    return $dst;
}
<?php

list($width, $height) = getimagesize($file);
$ratio = $width / $height;
if( $ratio > 1) {
    $resized_width = 500; //suppose 500 is max width or height
    $resized_height = 500/$ratio;
}
else {
    $resized_width = 500*$ratio;
    $resized_height = 500;
}

    if ($imageFileType == 'png') {
        $image = imagecreatefrompng($newfile);
    } else if ($imageFileType == 'gif') {
        $image = imagecreatefromgif($newfile);
    } else {
        $image = imagecreatefromjpeg($newfile);
    }

$resized_image = imagecreatetruecolor($resized_width, $resized_height);
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $resized_width, $resized_height, $width, $height);