zblog教程笔记:智能获取文章缩略图
作者:勇康博客网 | 分类:PHP | 浏览:1565 | 日期:2022年06月30日前言
最近在研究zblog框架,打算自己开发一个简约的主题。过程中打算提取文章缩略图,但是貌似zblog没有自带的方法,于是自己写了一个,很简单的实现。
功能
1、如果文章有图片,获取文章首图地址
2、如果首图获取失败,取随机图代替
代码
PHP
function yk_cool_summer_thumbnail($related) {
global $zbp;
$temp=mt_rand(1,10);
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
$content = $related->Content;
preg_match_all($pattern,$content,$matchContent);
if(isset($matchContent[1][0])){
$thumb=$matchContent[1][0];
}else{
$thumb=$zbp->host . "zb_users/theme/" .$zbp->theme. "/include/random/" .$temp. ".jpg";
}
$heads = get_headers($thumb, 1);
if(preg_match('/200/',$heads[0])){
return $thumb;
} else {
return $zbp->host . "zb_users/theme/" .$zbp->theme. "/include/random/" .$temp. ".jpg";
}
}