doushansu9012 2011-02-01 15:31
浏览 31

调整大小/裁剪图像的逻辑是否正确(在PHP中,但它是关于逻辑而不是代码)

Hey everyone, I have a php script to resize images to a certain size, if the aspect is the same it just resizes them, but if it's different it crops them first. I just wanted to check my logic was correct and that this would calculate the relevant dimensions for all source and destination image sizes:

$sourceratio = $actualsourcewidth / $actualsourceheight;
$targetratio = $targetwidth / $targetheight;

if ($targetratio < $sourceratio)
{
    $srcheight = $actualsourceheight;
    $srcwidth = $actualsourceheight * $targetratio;
    $srcy = 0;
    $srcx = floor(($actualsourcewidth - $srcwidth) / 2);
    $srcwidth = floor($srcwidth);
} else if ($targetratio > $sourceratio)
{
    $srcwidth = $actualsourcewidth;
    $srcheight = $actualsourcewidth / $targetratio;
    $srcy = floor(($actualsourceheight - $srcheight) / 2);
    $srcx = 0;
    $srcheight = floor($srcheight);
} else
{
// Same aspect ratio so you can resize the image without cropping
    $srcheight = $actualsourceheight;
    $srcwidth = $actualsourcewidth;
    $srcx = 0;
    $srcy = 0;
}

From what i can work out this should catch all eventualities and produce starting x, y coordinates ($srcx and $srcy) and source dimensions ($srcwidth, $srcheight) which can then be passed into imagecopyresampled.

The main thing I wanted to check was that the ratio check will prevent $srcheight and $srcwidth from ever being larger than the original width/height as this would break it, but I don't think it will?

Thanks very much as ever everyone!

Dave

  • 写回答

1条回答 默认 最新

  • duannao3819 2011-02-01 16:08
    关注

    It seems to work. I would refactor it some, initializing the variables and extracting them. This eliminates the need for the last else block and makes the code cleaner to read.

    $sourceratio = $actualsourcewidth / $actualsourceheight;
    $targetratio = $targetwidth / $targetheight;
    $srcx = 0;
    $srcy = 0;
    $srcheight = $actualsourceheight;
    $srcwidth = $actualsourcewidth;
    
    if ($targetratio < $sourceratio)
    {
        $srcwidth = $actualsourceheight * $targetratio;
        $srcx = floor(($actualsourcewidth - $srcwidth) / 2);
        $srcwidth = floor($srcwidth);
    } else if ($targetratio > $sourceratio)
    {
        $srcheight = $actualsourcewidth / $targetratio;
        $srcy = floor(($actualsourceheight - $srcheight) / 2);
        $srcheight = floor($srcheight);
    }
    

    If you want to be absolutely sure that $srcwidth and $srcheight do not exceed the originals, you could always clamp their values.

    $srcheight = min($actualsourceheight, floor($srcheight));
    

    You could also test each scenario, as there are only a few possible variances.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么