1、调首图或者固定图

这里,我们如果内容有图片的话调用内容中第一个张图,如果没有图则调用固定设置的一张图。

1
2
3
4
5
6
7
8
9
10
11
12
{foreach $articles as $article}
{php}
$pattern="/<&#91;img|IMG&#93;.*?src=&#91;\'|\"&#93;(.*?(?:&#91;\.gif|\.jpg|\.png&#93;))&#91;\'|\"&#93;.*?&#91;\/&#93;?>/";
$content = $article->Content;
preg_match_all($pattern,$content,$matchContent);
if(isset($matchContent[1][0]))
$temp=$matchContent[1][0];
else
$temp=$zbp->host."zb_users/theme/$theme/images/itbulu.jpg";/*固定图片地址*/
{/php}
<a href="{$article.Url}" target="_blank"><img src="{$temp}" /></a>
{/foreach}

2、无图随机图片

如果我们文章中无图的话,那就调用预设的随机图片,可以随机设置5个或者10个,这样丰富一些。

1
2
3
4
5
6
7
8
9
10
{php}
$temp=mt_rand(1,5);
$pattern="/<&#91;img|IMG&#93;.*?src=&#91;\'|\"&#93;(.*?(?:&#91;\.gif|\.jpg|\.png&#93;))&#91;\'|\"&#93;.*?&#91;\/&#93;?>/";
$content = $article->Content;
preg_match_all($pattern,$content,$matchContent);
if(isset($matchContent[1][0]))
$temp=$matchContent[1][0];
else
$temp=$zbp->host."zb_users/theme/$theme/style/images/random/$temp.png";
{/php}

如果没有图片,我们就从在当前主题的/style/images/random/文件夹中调用5个准备好的png图片,名称分别是1.png,2.png,3.png,4.png,5.png。且如果需要多个我们可以修改上面的参数。

1
<img src="{$temp}" />

在调用图片的位置放上上面的调用,我们也可以给上面图片加上样式。参考地址:https://cloud.tencent.com/developer/article/1925188。

3、函数形式调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function zbp_thumbnail($related) {

global $zbp;

$temp=mt_rand(1,4);

$pattern="/<&#91;img|IMG&#93;.*?src=&#91;\'|\"&#93;(.*?(?:&#91;\.gif|\.jpg|\.png&#93;))&#91;\'|\"&#93;.*?&#91;\/&#93;?>/";

$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";

}

return $thumb;

}

调用

1
{zbp_thumbnail($article)}

我们也需要设置随机图的预存地址,这样没有图的时候也会随机调用。

4、调用第一张图并且裁剪

定义函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//缩略图

function zbp_thumbnail($id,$sltww, $slthh,$link) {

global $zbp,$article;

$article=GetPost((int)$id);

$random = mt_rand(1, 10);

preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=&#91;\'"&#93;?(.+?)&#91;\'"&#93;?(?:(?: |\\t|\\r|\\n)+.*?)?\/>/sim', $article->Content, $strResult, PREG_PATTERN_ORDER);

$n = count($strResult[1]);

$zdsuoluetu=$article->Metas->Blogs_suoluetu;

if(empty($zdsuoluetu)){

if($n > 0){

$sltu=$strResult[1][0];

} else {

$sltu="{$zbp->host}zb_users/theme/{$zbp->theme}/image/random/{$random}.jpg";

}

}else{

$sltu=$zdsuoluetu;

}

$sltu="<img src=\"{$zbp->host}zb_users/theme/{$zbp->theme}/template/timthumb.php?src={$sltu}&w={$sltww}&h={$slthh}&zc=1\" alt=\"{$article->Title}\" />";

if($link==1){

$sltu="<a href=\"{$article->Url}\" title=\"{$article->Title}\">{$sltu}</a>";

}

return $sltu;

}