PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > CMS教程 > phpcms教程 > 正文

    phpcms thumb函数生成等比缩略图时变形的解决方法

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:phpcms thumb函数生成缩略图时,如果生成的比例一样如1000*500的图生成100*50的图时会变形的解决方案:如模板代码为:<img src="{thumb($...

    phpcms thumb函数生成缩略图时,如果生成的比例一样如1000*500的图生成100*50的图时会变形的解决方案:

    如模板代码为:
    <img src="{thumb($r[url],150,120,0)}" alt="{$r[alt]}" title="{$r[alt]}"/>


    图片为960*768时,生成150*120时就会变形(这是我实际开发过程中用到的代码)


    找到phpcms/libs/images.class.php中找到getpercent函数(大数在49行),把此函数中的
    $h = $dstw;
    $w = $dsth;


    改为
    $h = $dsth;
    $w = $dstw;


    下面是改过后的整个函数,看代码注释

        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;
        }
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-101-2079-1.html
    相关热词搜索: