【问题标题】:how to resize an image while maintaining width and height ratio如何在保持宽度和高度比的同时调整图像大小
【发布时间】:2020-10-20 00:41:24
【问题描述】:

我正在使用这个函数来调整上传图片的大小:

function imageResize($imageResourceId,$width,$height) {

    $targetWidth =750;
    $targetHeight =300;

    $targetLayer = imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);

    return $targetLayer;
}

但这会使图像宽度为 750,高度为 300。高度应该是灵活的并且取决于宽度。 所以我正在寻找这样的东西:

 $targetWidth = 750;
 $targetHeight = auto;

我真的不知道我该怎么做......

【问题讨论】:

  • 计算比率并乘以高度。 $targetHeight = $targetWidth/$width * $height;。我希望你能明白。
  • 现在效果很好!

标签: php image height width


【解决方案1】:

您可以轻松计算原始图像的比例并将其应用于调整大小的图像:

$targetHeight = ($height / $width) * $targetWidth

【讨论】:

    猜你喜欢
    • 2023-01-09
    • 2019-10-11
    • 2015-05-22
    • 2013-06-23
    • 2019-01-08
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多