QQ咨询 官方微信

添加微信好友

官方小程序

百度小程序

微信小程序

头条小程序

关于我们

PHPCMS

phpcms修改自动提取缩略图的字段,可同时判断多个字段

 admin  2022-06-03 19:30:04
官方默认的自动提取缩略图是从content字段中获取的,并且可以指定第几张图片,这种方法好处就是我们可以不用给缩略图单独上传一次图片了。
但是最近在做企业站的时候,因为要是用产品图轮播效果,经常会用到组图字段(pictureurls),这就造成了系统无法为我们提取缩略图,所以做了个改造:

(1)找到代码:phpcms\model\content_model.class.php,找到代码:
  1. //自动提取缩略图 
  2.         if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) { 
  3.             $content = $content ? $content : stripslashes($modelinfo['content']); 
  4.             $auto_thumb_no = intval($_POST['auto_thumb_no'])-1; 
  5.             if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) { 
  6.                 $systeminfo['thumb'] = $matches[3][$auto_thumb_no]; 
  7.             } 
  8.         } 

改成:

  1. //自动提取缩略图 
  2.         if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) { 
  3.             $content = $content ? $content : stripslashes($modelinfo['content']); 
  4.             $auto_thumb_no = intval($_POST['auto_thumb_no'])-1; 
  5.             if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) { 
  6.                 $systeminfo['thumb'] = $matches[3][$auto_thumb_no]; 
  7.             } 
  8.             if(empty($systeminfo['thumb'])  && array_key_exists("pictureurls",$data)){ //判断有无缩略图并且pictureurls字段是否存在
  9.                 $data['pictureurls'] = string2array($modelinfo['pictureurls']); 
  10.                 $systeminfo['thumb'] = $data['pictureurls'][0][url]; 
  11.             } 
  12.         } 

主要增加的代码是:
if(empty($systeminfo['thumb'])  && array_key_exists("pictureurls",$data)){
    $data['pictureurls'] = string2array($modelinfo['pictureurls']);
    $systeminfo['thumb'] = $data['pictureurls'][0][url];
 }
首先判断thumb是否已经生成,并且pictureurls是否存在,防止报错,pictureurl可以改成自己的其他字段

(2)找到该文件中的另外一处代码275行左右

  1. //自动提取缩略图 
  2.         if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) { 
  3.             $content = $content ? $content : stripslashes($modelinfo['content']); 
  4.             $auto_thumb_no = intval($_POST['auto_thumb_no'])-1; 
  5.             if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) { 
  6.                 $systeminfo['thumb'] = $matches[3][$auto_thumb_no]; 
  7.             } 
  8.         } 

改成:

  1. //自动提取缩略图 
  2.         if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) { 
  3.             $content = $content ? $content : stripslashes($modelinfo['content']); 
  4.             $auto_thumb_no = intval($_POST['auto_thumb_no'])-1; 
  5.             if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) { 
  6.                 $systeminfo['thumb'] = $matches[3][$auto_thumb_no]; 
  7.             } 
  8.             if(empty($systeminfo['thumb'])  && array_key_exists("pictureurls",$data)){ 
  9.                 $data['pictureurls'] = string2array($modelinfo['pictureurls']); 
  10.                 $systeminfo['thumb'] = $data['pictureurls'][0][url]; 
  11.             } 
  12.         } 


两处修改方式完全一样,这样我们在保存文章的时候,会自动从pictureurls字段中读取第一个图片作为缩略图,无需再次上传图片了。
 

¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《phpcms修改自动提取缩略图的字段,可同时判断多个字段》发布于石头博客文章,作者:admin,如若转载,请注明出处:https://www.pweb123.com/cms/phpcms/808.html,否则禁止转载,谢谢配合!

文章点评

我来说两句 已有0条评论
点击图片更换

添加微信好友

添加微信好友

微信小程序

百度小程序