但是最近在做企业站的时候,因为要是用产品图轮播效果,经常会用到组图字段(pictureurls),这就造成了系统无法为我们提取缩略图,所以做了个改造:
(1)找到代码:phpcmsmodelcontent_model.class.php,找到代码:
- //自动提取缩略图
- if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {
- $content = $content ? $content : stripslashes($modelinfo['content']);
- $auto_thumb_no = intval($_POST['auto_thumb_no'])-1;
- if(preg_match_all("/(src)=(["|']?)([^ "'>]+.(gif|jpg|jpeg|bmp|png))\2/i", $content, $matches)) {
- $systeminfo['thumb'] = $matches[3][$auto_thumb_no];
- }
- }
改成:
- //自动提取缩略图
- if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {
- $content = $content ? $content : stripslashes($modelinfo['content']);
- $auto_thumb_no = intval($_POST['auto_thumb_no'])-1;
- if(preg_match_all("/(src)=(["|']?)([^ "'>]+.(gif|jpg|jpeg|bmp|png))\2/i", $content, $matches)) {
- $systeminfo['thumb'] = $matches[3][$auto_thumb_no];
- }
- if(empty($systeminfo['thumb']) && array_key_exists("pictureurls",$data)){ //判断有无缩略图并且pictureurls字段是否存在
- $data['pictureurls'] = string2array($modelinfo['pictureurls']);
- $systeminfo['thumb'] = $data['pictureurls'][0][url];
- }
- }
主要增加的代码是:
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行左右
- //自动提取缩略图
- if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {
- $content = $content ? $content : stripslashes($modelinfo['content']);
- $auto_thumb_no = intval($_POST['auto_thumb_no'])-1;
- if(preg_match_all("/(src)=(["|']?)([^ "'>]+.(gif|jpg|jpeg|bmp|png))\2/i", $content, $matches)) {
- $systeminfo['thumb'] = $matches[3][$auto_thumb_no];
- }
- }
改成:
- //自动提取缩略图
- if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {
- $content = $content ? $content : stripslashes($modelinfo['content']);
- $auto_thumb_no = intval($_POST['auto_thumb_no'])-1;
- if(preg_match_all("/(src)=(["|']?)([^ "'>]+.(gif|jpg|jpeg|bmp|png))\2/i", $content, $matches)) {
- $systeminfo['thumb'] = $matches[3][$auto_thumb_no];
- }
- if(empty($systeminfo['thumb']) && array_key_exists("pictureurls",$data)){
- $data['pictureurls'] = string2array($modelinfo['pictureurls']);
- $systeminfo['thumb'] = $data['pictureurls'][0][url];
- }
- }
两处修改方式完全一样,这样我们在保存文章的时候,会自动从pictureurls字段中读取第一个图片作为缩略图,无需再次上传图片了。