最新消息: 新版网站上线了!!!

dede缩图功能并且不变形的方法

<img src="[field:litpic function=thumb('@me',126,84)/]" />
<?php
define('WEB_PUBLIC_PATH','../images/');
function litimgurls($imgid=0)
{
    global $lit_imglist,$dsql;
    //获取附加表
    $row = $dsql->GetOne("SELECT c.addtable FROM #@__archives AS a LEFT JOIN #@__channeltype AS c 
                                                            ON a.channel=c.id where a.id='$imgid'");
    $addtable = trim($row['addtable']);
    
    //获取图片附加表imgurls字段内容进行处理
    $row = $dsql->GetOne("Select imgurls From `$addtable` where aid='$imgid'");
    
    //调用inc_channel_unit.php中ChannelUnit类
    $ChannelUnit = new ChannelUnit(2,$imgid);
    
    //调用ChannelUnit类中GetlitImgLinks方法处理缩略图
    $lit_imglist = $ChannelUnit->GetlitImgLinks($row['imgurls']);
    
    //返回结果
    return $lit_imglist;
}
 
function thumb($f, $tw=60, $th=60 ,$autocat=0, $nopic = 'nopic.gif',$t=''){
    $f = str_replace('/upload','../upload',$f);
    if(file_exists($f)==false)
    {
    $f = str_replace('../upload','../../upload',$f);
    }
    if(file_exists($f)==false)
    {
    $f = str_replace('../../upload','../../../upload',$f);
    }
    if(empty($f)) return WEB_PUBLIC_PATH.$nopic;
    $pathinfo = pathinfo($f);
    if(empty($t)){
        $t = $pathinfo['dirname'].'/t_'.$tw.'_'.$th.'_'.$pathinfo['basename'];
        if(is_file($t)){
            return  $t;
        }
    }
    
    $temp = array(1=>'gif', 2=>'jpeg', 3=>'png');
    list($fw, $fh, $tmp) = getimagesize($f);
    if(!$temp[$tmp]){    return false; }
 
    if($autocat){
        if($fw/$tw > $fh/$th){
        $fw = $tw * ($fh/$th);
        }else{
        $fh = $th * ($fw/$tw);
        }
    }else{
 
         $scale = min($tw/$fw, $th/$fh); // 计算缩放比例
            if($scale>=1) {
                // 超过原图大小不再缩略
                $tw   =  $fw;
                $th  =  $fh;
            }else{
                // 缩略图尺寸
                $tw  = (int)($fw*$scale);
                $th = (int)($fh*$scale);
            }
 
 
    }
 
    $tmp = $temp[$tmp];
    $infunc = "imagecreatefrom$tmp";
    $outfunc = "image$tmp";
    $fimg = $infunc($f);
 
    if($tmp != 'gif' && function_exists('imagecreatetruecolor')){
        $timg = imagecreatetruecolor($tw, $th);
    }else{
        $timg = imagecreate($tw, $th);
    }
 
    if(function_exists('imagecopyresampled'))
        imagecopyresampled($timg, $fimg, 0,0, 0,0, $tw,$th, $fw,$fh);
    else
        imagecopyresized($timg, $fimg, 0,0, 0,0, $tw,$th, $fw,$fh);
    if($tmp=='gif' || $tmp=='png') {
        $background_color  =  imagecolorallocate($timg,  0, 255, 0);  //  指派一个绿色
        imagecolortransparent($timg, $background_color);  //  设置为透明色,若注释掉该行则输出绿色的图
    }
    $outfunc($timg, $t);
    imagedestroy($timg);
    imagedestroy($fimg);
    return $t;
}

转载请注明:谷谷点程序 » dede缩图功能并且不变形的方法