等比缩放图片函数

/**
 +------------------------------------------------------------------------------
 *                等比例压缩图片
 +------------------------------------------------------------------------------
 * @param String $src_imagename 源文件名        比如 “source.jpg”
 * @param int    $maxwidth      压缩后最大宽度
 * @param int    $maxheight     压缩后最大高度
 * @param String $savename      保存的文件名    “d:save”
 * @param String $filetype      保存文件的格式 比如 ”.jpg“
 * @author Yovae     <yovae@qq.com>
 * @version 1.0
 +------------------------------------------------------------------------------
 */
function resizeImage($src_imagename,$maxwidth,$maxheight,$savename,$filetype)
{
    $im=imagecreatefromjpeg($src_imagename);
    $current_width = imagesx($im);
    $current_height = imagesy($im);
 
    if(($maxwidth && $current_width > $maxwidth) || ($maxheight && $current_height > $maxheight))
    {
        if($maxwidth && $current_width>$maxwidth)
        {
            $widthratio = $maxwidth/$current_width;
            $resizewidth_tag = true;
        }
 
        if($maxheight && $current_height>$maxheight)
        {
            $heightratio = $maxheight/$current_height;
            $resizeheight_tag = true;
        }
 
        if($resizewidth_tag && $resizeheight_tag)
        {
            if($widthratio<$heightratio)
                $ratio = $widthratio;
            else
                $ratio = $heightratio;
        }
 
        if($resizewidth_tag && !$resizeheight_tag)
            $ratio = $widthratio;
        if($resizeheight_tag && !$resizewidth_tag)
            $ratio = $heightratio;
 
        $newwidth = $current_width * $ratio;
        $newheight = $current_height * $ratio;
 
        if(function_exists("imagecopyresampled"))
        {
            $newim = imagecreatetruecolor($newwidth,$newheight);
               imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$current_width,$current_height);
        }
        else
        {
            $newim = imagecreate($newwidth,$newheight);
           imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$current_width,$current_height);
        }
 
        $savename = $savename.$filetype;
        imagejpeg($newim,$savename);
        imagedestroy($newim);
    }
    else
    {
        $savename = $savename.$filetype;
        imagejpeg($im,$savename);
    }          
}

 

posted @ 2014-09-20 10:54  lovefuwei  阅读(181)  评论(0)    收藏  举报