下载鸥 > 网站下载 > 开发教程 > 帝国CMS

怎么去掉帝国CMS自动缩略图出现黑边框背景

466 2021-01-29 11:28:10

收藏

近期优化网站解决的的问题,帝国CMS自动缩略图会出现黑边框背景,很丑。看了GD库,发现没什么改的,网上找了下方法,完美解决了,分享下。

sys_ResizeImg函数去黑边方法

第一种方式 帝国默认:
sys_ResizeImg($r[titlepic],宽,高,0);//帝国默认的不裁剪缩放生成缩略图的方式
第二种方式 帝国默认:
sys_ResizeImg($r[titlepic],宽,高,1);//帝国默认的裁剪缩放生成缩略图的方式
第三种方式去掉裁剪不够时的黑边并且从图片缩放后中间裁剪:
sys_ResizeImg($r[titlepic],宽,高,2);//新加去黑边裁剪生成缩略图的方式
第四种方式 只固定图片的宽,高度不限制(类似不规则瀑布流的图片形式),高填写为大于0的任意整数数字:
sys_ResizeImg($r[titlepic],宽,高,3);//新加去黑边生成类似瀑布流格式的方式

/e/class/gd.php 函数全部代码如下:

<?php
define('InEmpireCMSGd',TRUE);

//原文件,新文件,宽度,高度,维持比例
functionResizeImage($big_image_name,$new_name,$max_width=400,$max_height=400,$resize=1){
$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_widthand$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;
$new_width=($ratiow>1)?$big_width:$max_width;
$new_height=($ratioh>1)?$big_height:$max_height;
if($resize==1){
if($big_width>=$max_widthand$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){//同比例缩放超出裁切
if($big_width>=$max_widthand$big_height>=$max_height)
{
if($max_width>($big_width*$ratioh)){
$tempx=$big_width;
$tempy=$max_height/$ratiow;
$srcX=0;
$srcY=($big_height-$tempy)/2;
}elseif($max_height>($big_height*$ratiow)){
$tempx=$max_width/$ratioh;
$tempy=$big_height;
$srcX=($big_width-$tempx)/2;
$srcY=0;
}
}else{
if($max_height>$big_height){
$tempx=$max_width;
$tempy=$big_height;
$srcX=($big_width-$max_width)/2;
$srcY=0;
}elseif($max_width>$big_width){
$tempx=$big_width;
$tempy=$max_height;
$srcX=0;
$srcY=($big_height-$max_height)/2;
}
}
}elseif($resize==3){//宽度固定高度同比例任意
$srcX=0;
$srcY=0;
$tempx=$big_width;
$tempy=$big_height;
if($big_width>=$max_width){
$new_height=$big_height*$ratiow;
}else{
$new_height=$big_height;
}
}else{//不保持比例
$srcX=0;
$srcY=0;
$tempx=$big_width;
$tempy=$big_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;
}

/*
*功能:图片加水印(水印支持图片或文字)
*参数:
*$groundImage背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
*$waterPos水印位置,有10种状态,0为随机位置;
*1为顶端居左,2为顶端居中,3为顶端居右;
*4为中部居左,5为中部居中,6为中部居右;
*7为底端居左,8为底端居中,9为底端居右;
*$waterImage图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
*$waterText文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
*$textFont文字大小,值为1、2、3、4或5,默认为5;
*$textColor文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
*
*注意:SupportGD2.0,SupportFreeType、GIFRead、GIFCreate、JPG、PNG
*$waterImage和$waterText最好不要同时使用,选其中之一即可,优先使用$waterImage。
*当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。
*加水印后的图片的文件名和$groundImage一样。
*/
functionimageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000",$myfontpath="../data/mask/cour.ttf",$w_pct,$w_quality){
global$fun_r,$editor;
if($editor==1){$a='../';}
elseif($editor==2){$a='../../';}
elseif($editor==3){$a='../../../';}
else{$a='';}
$waterImage=$waterImage?$a.$waterImage:'';
$myfontpath=$myfontpath?$a.$myfontpath:'';
$isWaterImage=FALSE;
$formatMsg=$fun_r['synotdotype'];

//读取水印文件
if(!empty($waterImage)&&file_exists($waterImage))
{
$isWaterImage=TRUE;
$water_info=getimagesize($waterImage);
$water_w=$water_info[0];//取得水印图片的宽
$water_h=$water_info[1];//取得水印图片的高

switch($water_info[2])//取得水印图片的格式
{
case1:$water_im=imagecreatefromgif($waterImage);break;
case2:$water_im=imagecreatefromjpeg($waterImage);break;
case3:$water_im=imagecreatefrompng($waterImage);break;
default:echo$formatMsg;return"";
}
}

//读取背景图片
if(!empty($groundImage)&&file_exists($groundImage))
{
$ground_info=getimagesize($groundImage);
$ground_w=$ground_info[0];//取得背景图片的宽
$ground_h=$ground_info[1];//取得背景图片的高

switch($ground_info[2])//取得背景图片的格式
{
case1:$ground_im=imagecreatefromgif($groundImage);break;
case2:$ground_im=imagecreatefromjpeg($groundImage);break;
case3:$ground_im=imagecreatefrompng($groundImage);break;
default:echo$formatMsg;return"";
}
}
else
{
echo$fun_r['synotdoimg'];
return"";
}

//水印位置
if($isWaterImage)//图片水印
{
$w=$water_w;
$h=$water_h;
$label="图片的";
}
else//文字水印
{
$temp=imagettfbbox(ceil($textFont*2.5),0,$myfontpath,$waterText);//取得使用TrueType字体的文本的范围
$w=$temp[2]-$temp[6];
$h=$temp[3]-$temp[7];
unset($temp);
$label="文字区域";
}
if(($ground_w<$w)||($ground_h<$h))
{
echo$fun_r['sytoosmall'];
return'';
}
switch($waterPos)
{
case0://随机
$posX=rand(0,($ground_w-$w));
$posY=rand(0,($ground_h-$h));
break;
case1://1为顶端居左
$posX=0;
$posY=0;
break;
case2://2为顶端居中
$posX=($ground_w-$w)/2;
$posY=0;
break;
case3://3为顶端居右
$posX=$ground_w-$w;
$posY=0;
break;
case4://4为中部居左
$posX=0;
$posY=($ground_h-$h)/2;
break;
case5://5为中部居中
$posX=($ground_w-$w)/2;
$posY=($ground_h-$h)/2;
break;
case6://6为中部居右
$posX=$ground_w-$w;
$posY=($ground_h-$h)/2;
break;
case7://7为底端居左
$posX=0;
$posY=$ground_h-$h;
break;
case8://8为底端居中
$posX=($ground_w-$w)/2;
$posY=$ground_h-$h;
break;
case9://9为底端居右
$posX=$ground_w-$w;
$posY=$ground_h-$h;
break;
default://随机
$posX=rand(0,($ground_w-$w));
$posY=rand(0,($ground_h-$h));
break;
}

//设定图像的混色模式
imagealphablending($ground_im,true);

if($isWaterImage)//图片水印
{
if($water_info[2]==3)
{
imagecopy($ground_im,$water_im,$posX,$posY,0,0,$water_w,$water_h);//拷贝水印到目标文件
}
else
{
imagecopymerge($ground_im,$water_im,$posX,$posY,0,0,$water_w,$water_h,$w_pct);//拷贝水印到目标文件
}
}
else//文字水印
{
if(!empty($textColor)&&(strlen($textColor)==7))
{
$R=hexdec(substr($textColor,1,2));
$G=hexdec(substr($textColor,3,2));
$B=hexdec(substr($textColor,5));
}
else
{
echo$fun_r['synotfontcolor'];
return"";
}
imagestring($ground_im,$textFont,$posX,$posY,$waterText,imagecolorallocate($ground_im,$R,$G,$B));
}

//生成水印后的图片
@unlink($groundImage);
switch($ground_info[2])//取得背景图片的格式
{
case1:imagegif($ground_im,$groundImage);break;
case2:imagejpeg($ground_im,$groundImage,$w_quality);break;
case3:imagepng($ground_im,$groundImage);break;
default:echo$formatMsg;return"";
}

//释放内存
if(isset($water_info))unset($water_info);
if(isset($water_im))imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
?>
后台自动缩略图去黑边方法

系统参数-系统参数设置-图片设置-超出部分是否截取,去掉勾选

本文地址:https://xzo.com.cn/develop/empire/188.html

有帮助,很赞!

信息来源:精准像素
导出教程 下载word版教程
发表评论 共有条评论
关于帝国CMS


帝国cms是一款功能极为强大的cms程序,性能强悍、安全性高,可轻松支持10万数据,高级开发人员可制作出能容纳千万数据量的网站,是国内最出色的开源cms程序之一,推荐企业用户使用

当前最新版本为7.5,8.0版本即将上线,新版本的核心优化点在于多终端的适配。

推荐帝国CMS开发教程
帝国cms取消用户邮箱必填(注册/修改资料/管理员编辑资料)
帝国cms取消用户邮箱必填(注册/修改资料/管理员编辑资料)

网上关于取消会员邮箱必填的教程很多,但大都不完整。今天放出完整版本,确保全...

12 809
帝国CMS留言板怎样增加字段?
帝国CMS留言板怎样增加字段?

网站留言板是用户留存的重要环节,但帝国cms自带的留言板功能十分单一,大多数...

1 484
帝国CMS生成选项是“ID目录”无斜扛解决的方法
帝国CMS生成选项是“ID目录”无斜扛解决的方法

如果你栏目生成选项选择的ID目录,那么帝国他生成的地址会少一个斜扛,访问时就...

3 554
帝国cms怎样记录会员浏览信息历史记录?
帝国cms怎样记录会员浏览信息历史记录?

帝国cms论坛早已有了以cookie形式保存浏览量历史记录在客户端的插件,但这样...

7 602
帝国cms怎样批量删除上万篇文章/指定会员/时间段发布的信息?
帝国cms怎样批量删除上万篇文章/指定会员/时间段发布的信息?

在网站运营过程中,我们可能因为这样那样的原因需要批量删除大批文章信息,几十...

1 1329
帝国cms某个栏目使用完全自定义的分页功能
帝国cms某个栏目使用完全自定义的分页功能

帝国CMS列表页的分页函数是用系统设置中的分页函数,修改都是在系统设置里进...

0 823
帝国cms怎样分享到微信好友、朋友圈带图片与简介
帝国cms怎样分享到微信好友、朋友圈带图片与简介

微信作为我们的国民app流量很大,简单的js分享往往只有一个二维码或者链接,但...

8 509
帝国cms 使用download.js下载视频文件
帝国cms 使用download.js下载视频文件

视频文件默认会被pc浏览器直接打开,此时download属性也同样无效。那么,要怎样...

0 572
推荐插件
帝国cms批量添加后台用户插件
帝国cms批量添加后台用户插件

使用帝国cms的企业用户、新闻资讯类站点的用户很多,此类站点很多时候需要有...

0 899
帝国cms百度AI图像无损放大api接口对接插件
帝国cms百度AI图像无损放大api接口对接插件

通过本插件,可以实现帝国cms网站对接百度云api实现图像无损放大的功能。经过...

0 551
帝国cms多栏目多数据表自动审核推送插件
帝国cms多栏目多数据表自动审核推送插件

本插件基于帝国cms帝国cms每日自动审核插件,在自动审核指定条数信息的基础上...

0 1042
帝国cms访问统计ip地址链接与封禁插件下载
帝国cms访问统计ip地址链接与封禁插件下载

下载鸥开发了这款帝国cms封禁ip插件,自动记录访问情况,让我们可以更快的识别...

0 1225
帝国cms百度AI黑白图像上色api接口对接插件
帝国cms百度AI黑白图像上色api接口对接插件

百度开放了系列AI功能api如图像上色、图像去雾、图像修复、无损放大、清晰...

0 527
帝国cms智能自动审核按星期几审核指定栏目带推送插件
帝国cms智能自动审核按星期几审核指定栏目带推送插件

采集站的必备资源是自动审核,要做到日收录也离不开定时发布。而本插件的自动...

0 1415
帝国CMS内网用户静态站点文章访客统计插件
帝国CMS内网用户静态站点文章访客统计插件

本插件适用于内网用户,可查看单篇文章访问者ip地址。如果添加访问者ip组,可查...

0 666
帝国cms自动生成文章新闻目录插件下载
帝国cms自动生成文章新闻目录插件下载

用户体验是我们的需求,百度蜘蛛的认可更是我们的需求。毕竟,没有收录排名,何来...

0 1265
客服QQ:341553759
扫码咨询 常见问题 >
官方交流群:90432500
点击加入