Php 图像重采样维度问题

Php image resampling dimension issue

我正在使用下面的 php 图片功能在上传或在页面上显示图片时调整图片大小,但我发现当我尝试调整原始图片大小时它没有给我我想要的实际尺寸。

示例:原图宽度为600,高度为400 我打算将它的大小调整为 180 X 180,但它给了我宽度 180 和高度 120。请问我不知道是什么问题有人可以帮忙吗?

<?php
function CroppedThumbnail($imagename, $meta, $newpath, $imgwidth, $imgheight, $rename, $imgQuality){
// Set a maximum height and width
    $width = $imgwidth;
    $height = $imgheight;
    $url = '';
//Get image info

    $info = @getimagesize($imagename);
    $fileparts = pathinfo($imagename);
// Get new dimensions
    $imageAvatar =  substr($fileparts['filename'],0,5) . '-' . $imgwidth . 'x' . $imgheight . '.png';
    if(is_dir($newpath . '/') && file_exists($newpath . '/' . $imageAvatar)){
       return $url . $newpath . '/' . $imageAvatar;
    }else{      
    list($width_orig, $height_orig) = $info;
    $ratio_orig = $width_orig/$height_orig;
    if ($width/$height > $ratio_orig) {
        $width = $height*$ratio_orig;
    } else {
        $height = $width/$ratio_orig;
    }
// Resample
    if ($info['mime'] == 'image/jpeg'){
        $image = imagecreatefromjpeg($url.$imagename);
    }
    else if ($info['mime'] == 'image/gif'){
        $image = imagecreatefromgif($url.$imagename);
    }
    else if ($info['mime'] == 'image/png'){
        $image = imagecreatefrompng($url.$imagename);
    }
    $image_p = imagecreatetruecolor($width, $height);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
//Rename the image
    if($rename == true){
        $newName = substr($fileparts['filename'],0,5) . '-' . $imgwidth . 'x' . $imgheight;
        $newFileName = $newpath . '/' . $newName . '.png';
    }else{
        $newFileName = $newpath . '/' . $imagename;
    }
// Output
    imagejpeg($image_p, $newFileName, $imgQuality);
    return $url . $newFileName;
    }
}

这是因为您保持了图片的原始纵横比。

这是在下面的代码行中:

$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
    $width = $height*$ratio_orig;
} else {
    $height = $width/$ratio_orig;
}

如果您不想保留纵横比,只需删除这些行即可。