POST TIME:2017-11-13 00:46
2013web成了讀圖世代,隨著瀑布流布局的大行其道,圖片在網站關的重要性日漸上升,然而縮略圖作為文章的入口顯的更加重要,上篇跟版網介紹了dedecms解決縮略圖變形的問題,本篇將介紹縮略圖的另一個運用:dedecms多尺寸縮略圖的調用
dedecms系統本身只支持一種尺寸的縮略圖調用,即[field:litpic/],縮略圖的尺寸在網站后臺:DedeCMS系統配置參數》附件設置中可以設置大小,但一種尺寸的縮略圖勢必不能滿足我們的需求,比如說我們網站經常會用到幻燈圖片展示,幻燈圖片的尺寸一般都是比較大的,如果用縮略圖來作就不太合適,再如網站有生活風景,人物攝影這兩個欄目,兩類圖片的比例是不相同的,如果保有一種尺寸的縮略圖這樣就顯的很難看,下面介紹dedecms實現多種尺寸縮略圖調用的方法
打開/include/extend.func.php,在文件末尾添加以下代碼
function M_mkdirs($dir)
{
if(!is_dir($dir))
{
if(!M_mkdirs(dirname($dir))){
return false;
}
if(!mkdir($dir,0777)){
return false;
}
}
return true;
}
if (!function_exists('qmtx3Thumb'))
{
function qmtx3Thumb($aid, $width, $height, $n=0, $bg = false)
{
global $cfg_basehost,$cfg_multi_site,$dsql;
$query= "SELECT body FROM `dede_addonarticle` where aid='$aid'";
$row = $dsql->GetOne($query);
$preg = "/<img.*?src=[\&;\&;](.+?)[\&;\&;].*?>/i";
preg_match_all($preg, $row['body'], $match);
$imgurl = $match[1][0];
if($n==='L'){
$imgurl = end($match[1]);
}
if(!$imgurl) return '/images/defaultpic.gif';
$thumb = str_replace($cfg_basehost,'',$imgurl);
if(strpos($thumb, 'http://') === 0){
return '/images/defaultpic2.gif';
}
list($thumbname,$extname) = explode('.',$thumb);
$newImgdir = DEDEROOT.str_replace('uploads', 'uploads/diylitpic', str_replace($cfg_basehost, '', dirname($imgurl)));
$newthumb = str_replace('uploads', 'uploads/diylitpic', $thumbname).'_'.$width.'_'.$height.'.'.$extname;
$mdsure = M_mkdirs($newImgdir);
if($mdsure){
if(!$thumbname || !$extname || !file_exists(DEDEROOT.$thumb)) return $imgurl;
if(!file_exists(DEDEROOT.$newthumb)){
include_once DEDEINC.'/image.func.php';
if($bg==true)
{
ImageResizeNew(DEDEROOT.$thumb, $width, $height, DEDEROOT.$newthumb);
}
else
{
ImageResize(DEDEROOT.$thumb, $width, $height, DEDEROOT.$newthumb);
}
}
return $cfg_multi_site=='Y'?$cfg_basehost.$newthumb:$newthumb;
}else{
return '創建目錄失敗';
}
}
}
以上定義一個多級目錄創建函數M_mkdirs和一個縮略圖生成函數qmtx3Thumb($aid, $width, $height, $n=0, $bg = false)
qmtx3Thumb有5個參數$aid為文檔id,$width, $height為生成圖片的寬、高,$n代表用內容的第幾張大圖生成縮略圖,這里默認為0即第一張,$bg為是否強制縮略圖大小一般為false
然后在模板中需要調用縮略圖的地方中調用
{dede:arclist}
<img width="200" height="200" src="[field:id function='qmtx3Thumb(@me, 200, 200)'/]" />
{/dede:arclist}
注:如果你之前的縮略圖就是變形的,請移動“
本文來源:genban.org