duanjumie8753 2012-08-16 10:53
浏览 74
已采纳

计算新图像大小 - 如何使其更有效/更短

I have a script, that's pretty huge for an actual simple task, because it's nested very often. What it does is this:

  • get the current size of an image
  • get the new maximum dimensions
  • calculate the dimensions the image is going to be resized to (aspect ratio is always kept):
    • if image is smaller than new dimensions: keep original size
    • if max new dimensions have an x and y-value: scale image to fit both values
    • if max new dimensions have only a x-value: scale to fit at least x
    • if max new dimensions have only a y-value: scale to fit at least y

This is the script:

//example values, whereas 'new' is the maximum range for the new image
$dimensions = array(1920,1200);
$dimensionsNew  = array(160, 0);
// percentage the image must be scaled
$percentages    = array(0,0);

//calculate scale range
if ($dimensionsNew[0] != 0) $percentages[0] = $dimensions[0] / $dimensionsNew[0];
if ($dimensionsNew[1] != 0) $percentages[1] = $dimensions[1] / $dimensionsNew[1];

//get new dimensions
if ( ($dimensions[0] <= $dimensionsNew[0]) && ($dimensions[1] <= $dimensionsNew[1]) ) {
    $dimensionsNew[0]       = $dimensions[0];
    $dimensionsNew[1]       = $dimensions[1];
} elseif ($dimensionsNew[0] == 0) {
    if ($dimensions[1] <= $dimensionsNew[1]) {
        $dimensionsNew[0]   = $dimensions[0];
        $dimensionsNew[1]   = $dimensions[1];
    } else {
        $dimensionsNew[0]   = ceil($dimensions[0] / $percentages[1]);
    }
} elseif ($dimensionsNew[1] == 0) {
    if ($dimensions[0] <= $dimensionsNew[0]) {
        $dimensionsNew[0]   = $dimensions[0];
        $dimensionsNew[1]   = $dimensions[1];
    } else {
        $dimensionsNew[1]   = ceil($dimensions[1] / $percentages[0]);
    }
} elseif ($percentages[0] < $percentages[1]) {
    $dimensionsNew[1]       = ceil($dimensions[1] / $percentages[1]);
} else {
    $dimensionsNew[0]       = ceil($dimensions[0] / $percentages[0]);
}

It does what it should do. But is there an easier way of doing this? I think I looked at this code too often and can't find a way to optimize.

  • 写回答

1条回答 默认 最新

  • duanhan3067 2012-08-16 11:26
    关注

    You should use "min" on the rations of the sizes to work out the scale required. In this is >1 then no shrinking ncessary, if <1 then shrink by the right amount

    //example values, whereas 'new' is the maximum range for the new image
    $dimensions = array(1920,1200);
    $dimensionsNew  = array(160, 0);   // NOTE: 0 won't work, but it's your example ;)
    
    // What scale do we need to go to
    $scaleRequired = min($dimensionsNew[0] / $dimensions[0], $dimensionsNew[1] / $dimensions[1]);
    
    if ($scaleRequired < 1) {
        $finalDimensions = array (
             $dimensions[0] * $scaleRequired, 
             $dimensions[1] * $scaleRequired);
    
        // Resize to $finalDimensions
    
    } else {
        // Do nothing
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!