php圖片等比例縮放生成縮略圖函數(shù)分享

字號:


    代碼如下:
    <?php
    /*
    *@im //需要縮放的圖片資源
    *@filetype //制作的縮略圖文件類型
    *@dstimw //縮放的圖片的寬度
    *@dstimh //縮放的圖片的高度
    *@thumbname //縮略圖文件名字
    function makethumb($im,$dstimw,$dstimh,$thumbname ,$filetype){
    //獲取im的寬度和高度
    $pic_w=imagesx($im);
    $pic_h=imagesy($im);
    $arr = array();
    swith($filetype){
    case 'jpg':
    $arr[$filetype]=imagejpeg;
    break;
    case 'png';
    $arr[$filetype]=imagepng;
    break;
    case 'jif';
    $arr[$filetype]=imagegif;
    }
    if(($dstimgw && $dstimgw<$pic_w) || ($dstimgh && $dstimgh<$pic_h) ){
    if($dstimgw && $dstimgw<$pic_w){
    $dsimgwratio = $dstimgw / $pic_w;
    $resizereagw =true;
    }
    if($dstimgh && $ $dstimgh <$pic_h){
    $dsimghratio = $dstimgh/$pic_h;
    $resizerreagh =true;
    }
    //縮略圖寬高和原圖寬高比,取最小的那個(gè)
    if($resizereagw && $resizerreagh){
    if($dsimgwratio<$dsimghratio)
    $radio = $dsimgwratio;
    else
    $radio = $dsimghratio;
    }
    if($resizereagw && !$resizerreagh ){
    $radio = $dsimgwratio;
    }
    if(!$resizereagw && $resizerreagh){
    $radio = $dsimghratio ;
    }
    $imgneww = $pic_w * $radio;
    $imgnewh = $pic_h * $radio;
    if(function_exists(imgcopyresampled)){
    //創(chuàng)建目標(biāo)資源畫布
    $dst = imagecreatetruecolor ($imgneww, $imgnewh);
    imagecopyresampled ($dst,$im,0,0,0,0,$imgneww,$imgnewh,$pic_w,$pic_h);
    }else{
    $dst=imagecreate($imgneww, $imgnewh);
    imagecopyresized ($dst, $im,0,0,0,0,$imgneww,$imgnewh,$imgnewh,$pic_w,$pic_h);
    }
    $arr[$filetype]($dst,$thumbname..$filetype);
    imagedestroy ($dst);
    }else{//縮略圖自身的寬和高已經(jīng)大于了原圖的寬和高
    //則縮略圖的寬和縮略的高就是原圖的寬和原圖的高
    $arr[$filetype]($im,$thumbname..$filetype);
    imagedestroy();
    }
    }
    ?>