首页 > 其他 > 网站日记>正文

帝国cms之缩略图函数的修改-更好的支持瀑布流

提示: 阅读权限:公开  

这里修改帝国的缩略图生成函数,使得能更好的适应瀑布流对缩率图的需求。该函数位于e/class/gd.php中。

当高度参数为10000的时候,以宽度为基准等比例缩放,高度不限。

当高度参数大于1万的时候,比如为10400,其含义为:以宽度为基准等比例缩放,高度限制为400像素,超过部分底部截取(保留上部)。

函数修改后如下:

 

//原文件,新文件,宽度,高度,维持比例

function ResizeImage($big_image_name, $new_name, $max_width = 400, $max_height = 400, $resize = 1){
 if($max_height>10000){$max_height=$max_height-10000; $resize = 2;}

 $returnr['file']='';

 $returnr['filetype']='';

    if($temp_img_type = @getimagesize($big_image_name)) {preg_match('//([a-z]+)$/i', $temp_img_type[mime], $tpn); $img_type = $tpn[1];}

    else {preg_match('/.([a-z]+)$/i', $big_image_name, $tpn); $img_type = $tpn[1];}

    $all_type = array(

        "jpg"   => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg"  , "exn"=>".jpg"),

        "gif"   => array("create"=>"ImageCreateFromGIF" , "output"=>"imagegif"   , "exn"=>".gif"),

        "jpeg"  => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg"  , "exn"=>".jpg"),

        "png"   => array("create"=>"imagecreatefrompng" , "output"=>"imagepng"   , "exn"=>".png"),

        "wbmp"  => array("create"=>"imagecreatefromwbmp", "output"=>"image2wbmp" , "exn"=>".wbmp")

    );

 

    $func_create = $all_type[$img_type]['create'];

    if(empty($func_create) or !function_exists($func_create))

 {

  return $returnr;

 }

 //输出

    $func_output = $all_type[$img_type]['output'];

    $func_exname = $all_type[$img_type]['exn'];

 if(($func_exname=='.gif'||$func_exname=='.png'||$func_exname=='.wbmp')&&!function_exists($func_output))

 {

  $func_output='imagejpeg';

  $func_exname='.jpg';

 }

    $big_image   = $func_create($big_image_name);

    $big_width   = imagesx($big_image);

    $big_height  = imagesy($big_image);

    if($big_width <= $max_width and $big_height <= $max_height)

 {

  $func_output($big_image, $new_name.$func_exname);

  $returnr['file']=$new_name.$func_exname;

  $returnr['filetype']=$func_exname;

  return $returnr;

 }

    $ratiow      = $max_width  / $big_width;

    $ratioh      = $max_height / $big_height;
 // jiuhcecai add
 if($max_height == 10000){
  $ratioh=$ratiow;
     $max_height=$ratioh* $big_height;

 }
    if($resize == 1) {

        if($big_width >= $max_width and $big_height >= $max_height)

        {

            if($big_width > $big_height)

            {

            $tempx  = $max_width / $ratioh;

            $tempy  = $big_height;

            $srcX   = ($big_width - $tempx) / 2;

            $srcY   = 0;

            } else {

            $tempy  = $max_height / $ratiow;

            $tempx  = $big_width;

            $srcY   = ($big_height - $tempy) / 2;

            $srcX   = 0;

            }

        } else {

            if($big_width > $big_height)

            {

            $tempx  = $max_width;

            $tempy  = $big_height;

            $srcX   = ($big_width - $tempx) / 2;

            $srcY   = 0;

            } else {

            $tempy  = $max_height;

            $tempx  = $big_width;

            $srcY   = ($big_height - $tempy) / 2;

            $srcX   = 0;

            }

        }

    }elseif($resize == 2) // 以宽为准,适合瀑布流
 {
  $ratioh=$ratiow;
  $tempHeight=$ratiow*$big_height;
  $srcX      = 0;
        $srcY      = 0;
        $tempx     = $big_width;
        $tempy     = $big_height;
  if($tempHeight>$max_height)$tempy =$max_height/$ratiow;
        $max_height=$ratiow*$tempy;
 }else
 {
  
   

        $srcX      = 0;

        $srcY      = 0;

        $tempx     = $big_width;

        $tempy     = $big_height;

    }

 

    $new_width  = ($ratiow  > 1) ? $big_width  : $max_width;

    $new_height = ($ratioh  > 1) ? $big_height : $max_height;

    if(function_exists("imagecopyresampled"))

    {

        $temp_image = imagecreatetruecolor($new_width, $new_height);

        imagecopyresampled($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);

    } else {

        $temp_image = imagecreate($new_width, $new_height);

        imagecopyresized($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);

    }

        $func_output($temp_image, $new_name.$func_exname);

        ImageDestroy($big_image);

        ImageDestroy($temp_image);

  $returnr['file']=$new_name.$func_exname;

  $returnr['filetype']=$func_exname;

    return $returnr;

}

 

 

上一篇:中药方大全网编辑奖惩方案

下一篇:帝国cms在模板中修改字段值

tags: 帝国cms 帝国 缩略图 瀑布流 瀑布

返回首页

相关

热门

站内直通车

[!--temp.bottomnav--]
返回顶部