phpcms thumb函數(shù)生成縮略圖時(shí),如果生成的比例一樣如1000*500的圖生成100*50的圖時(shí)會(huì)變形的解決方案:
如模板代碼為:
<img src="{thumb($r[url],150,120,0)}" alt="{$r[alt]}" title="{$r[alt]}"/>
圖片為960*768時(shí),生成150*120時(shí)就會(huì)變形(這是我實(shí)際開(kāi)發(fā)過(guò)程中用到的代碼)
找到phpcms/libs/images.class.php中找到getpercent函數(shù)(大數(shù)在49行),把此函數(shù)中的
$h = $dstw;
$w = $dsth;
改為
$h = $dsth;
$w = $dstw;
下面是改過(guò)后的整個(gè)函數(shù),看代碼注釋
function getpercent($srcwidth,$srcheight,$dstw,$dsth) {
if (empty($srcwidth) || empty($srcheight) || ($srcwidth <= $dstW && $srcheight <= $dstH)) $w = $srcwidth ;$h = $srcheight;
if ((empty($dstw) || $dstw == 0) && $dsth > 0 && $srcheight > $dsth) {
$h = $dsth;
$w = round($dsth / $srcheight * $srcwidth);
} elseif ((empty($dsth) || $dsth == 0) && $dstw > 0 && $srcwidth > $dstw) {
$w = $dstw;
$h = round($dstw / $srcwidth * $srcheight);
} elseif ($dstw > 0 && $dsth > 0) {
if (($srcwidth / $dstw) < ($srcheight / $dsth)) {
$w = round($dsth / $srcheight * $srcwidth);
$h = $dsth;
}
elseif (($srcwidth / $dstw) > ($srcheight / $dsth)) {
$w = $dstw;
$h = round($dstw / $srcwidth *
$srcheight );
} else
//第一步:把以下兩行代碼注釋掉
//$h = $dstw;
//$w = $dsth;
//第二步:改為以下兩行代碼
$h = $dsth;
$w = $dstw;
}
}
$array['w'] = $w;
$array['h'] = $h;
return $array;
}
|
新聞熱點(diǎn)
疑難解答
圖片精選