PHP根据指定宽或高计算比例

<?php
/**
 * 根据指定宽或高计算比例
 */
function resizeImage($pic_width, $pic_height, $maxwidth,$maxheight)
{
    $CI =& get_instance();

    if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
    {
        if ($maxwidth && $pic_width > $maxwidth)
        {
            $widthratio = $maxwidth / $pic_width;
            $resizewidth_tag = true;
        }

        if ($maxheight && $pic_height > $maxheight)
        {
            $heightratio = $maxheight / $pic_height;
            $resizeheight_tag = true;
        }

        if ($resizewidth_tag && $resizeheight_tag)
        {
            // 指定宽度,如需根据高度计算比例,则修改成$widthratio>$heightratio
            if ($widthratio < $heightratio) $ratio = $widthratio;
            else $ratio = $heightratio;
        }

        if ($resizewidth_tag && !$resizeheight_tag) $ratio = $widthratio;
        if ($resizeheight_tag && !$resizewidth_tag) $ratio = $heightratio;

        $newwidth = $pic_width * $ratio;
        $newheight = $pic_height * $ratio;
        
        return array(
            'newwidth' => $newwidth,
            'newheight' => $newheight,
        );
    }
    
    return FALSE;
}
?>

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注