在使用帝国CMS构建商城系统时,上传大量产品图片后,有时需要调整缩略图大小。以下是一个经过修改的方法,确保适用于6.0版本的帝国CMS。

首先,在t_functions.php文件中添加两个函数:


function Titlepic_all ($id, $classid, $userid, $username, $pwidth, $pheight) {
    global $empire,$class_r,$class_zr,$dbtbpre,$keys;
    $count = count ($id);
    if (!$count) {
        printerror ('Noteditpic', 'history.go(-1)');
    }
    for ($i = 0; ($i < $count); ++$i) {
        $r = $empire->fetch1 ('select * from phome_ecms_'.$class_r[$classid][tbname].' where id='.$id[$i].' and titlepic<>""');
        if(!emptyempty($r[id])){
            ImageResize("../../".$r[titlepic],$pwidth,$pheight,"../../".$r[titlepic]);
        }
    }
    printerror ('TitlepicAllSuccess', $_SERVER['HTTP_REFERER']);
}

Function ImageResize($srcFile,$toW,$toH,$toFile="") {
    global $keys;
    if($toFile==""){ $toFile = substr($srcFile,0,-4)."small".substr($srcFile,-4); }
    $info = "";
    $data = GetImageSize($srcFile,$info);
    switch ($data[2]) {
        case 1:
            if(!function_exists("imagecreatefromgif")){
                echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回";
                exit();
            }
            $im = ImageCreateFromGIF($srcFile);
            break;
        case 2:
            if(!function_exists("imagecreatefromjpeg")){
                echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回";
                exit();
            }
            $im = ImageCreateFromJpeg($srcFile);
            break;
        case 3:
            $im = ImageCreateFromPNG($srcFile);
            break;
    }
    $srcW = ImageSX($im);
    $srcH = ImageSY($im);
    $keys= 0;
    if (($srcW>$toW) or ($srH>$toH)){
        if(($srcW/$toW)>=($srcH/$toH)){
            $temp_height=$toH;
            $temp_width=$srcW/($srcH/$toH);
            $src_X=Abs(($toW-$temp_width)/2);
            $src_Y=0;
        } else {
            $temp_width=$toW;
            $temp_height=$srcH/($srcW/$toW);
            $src_X=0;
            $src_Y=Abs(($toH-$temp_height)/2);
        }
        $temp_img=ImageCreateTrueColor($temp_width,$temp_height);
        imagecopyResampled($temp_img,$im,0,0,0,0,$temp_width,$temp_height,$srcW,$srcH);
        $ni=ImageCreateTrueColor($toW,$toH);
        imagecopyResampled($ni,$temp_img,0,0,$src_X,$src_Y,$toW,$toH,$toW,$toH);
        if(Function_exists('imagejpeg')) ImageJpeg($ni,$toFile);
        else ImagePNG($ni,$toFile);
        ImageDestroy($ni);
        $keys= 1;
    }
    ImageDestroy($im);
}

接下来,在e/admin/ListNews.php(还有e/admin/ListAllInfo.php)页面的最后几行“选中全部 ”后面添加以下代码:


缩略图宽: 缩略图高:

其中,图宽和图高的默认值可以根据实际需求进行设定,设置好后操作会更加方便。

然后,在e/admin/ecmsinfo.php中添加如下代码:

//批量加标题图片
elseif($enews=="Titlepic_all") {
    $id=$_POST@['id'];
    $classid=$_POST@['classid'];
    $bclassid=$_POST@['bclassid'];
    $pwidth=$_POST@['pwidth'];
    $pheight=$_POST@['pheight'];
    Titlepic_all($id,$classid,$logininid,$loginin,$pwidth,$pheight);
}

最后,在e/data/language/gb/pub/message.php中添加提示信息:

'Noteditpic'=>'没有选择!',
'TitlepicAllSuccess'=>'操作成功!',

这一步不是必须的,但可以提高用户体验。完成以上步骤后,您就可以轻松地批量调整缩略图大小了。