Home  >  Article  >  php教程  >  按照比例改变图片大小(非生成缩略图)

按照比例改变图片大小(非生成缩略图)

WBOY
WBOYOriginal
2016-06-21 08:49:57854browse

 


/** 
    按照比例改变图片大小(非生成缩略图) 
    @param string $img 图片路径 
    @param int $max_w 最大缩放宽 
    @param int $max_h 最大缩放高 
*/ 
function chImageSize ($img,$max_w,$max_h) 

    $size = @getimagesize($img); 
        $w = $size[0]; 
        $h     =    $size[1]; 
    //计算缩放比例 
    @$w_ratio = $max_w / $w; 
    @$h_ratio =    $max_h / $h; 
    //决定处理后的图片宽和高 
    if( ($w     { 
        $tn['w'] = $w; 
        $tn['h'] = $h; 
    } 
    else if(($w_ratio * $h)     { 
        $tn['h'] = ceil($w_ratio * $h); 
        $tn['w'] = $max_w; 
    } 
    else 
    { 
        $tn['w'] = ceil($h_ratio * $w); 
        $tn['h'] = $max_h; 
    } 
    $tn['rc_w'] = $w; 
    $tn['rc_h'] = $h; 
    return $tn ; 
}
?>


函数描述及例子

按照比例改变图片大小(非生成缩略图)



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn