目前我们看到的个人博客主题中较多的还是较为多选择图文结合的,那种有缩略图列表式的用户比较喜欢。当然有时候在做企业网站的时候产品图片也是采用的这样方式,产品的缩略图有些时候是特定的字段调用,有些也是直接调用产品内容中的第一张图。在这篇文章中,老蒋整理出来我们常用ZBLOG程序做企业网站时候,或者有些网站调用缩略图的方法。便于我们在ZBLOG模板制作的时候使用。
1、调首图或者固定图
这里,我们如果内容有图片的话调用内容中第一个张图,如果没有图则调用固定设置的一张图。

{foreach $articles as $article}
{php}
$pattern="/<[img|IMG].?src=\'|\")[\'|\"].?[/]?>/";
$content = $article->Content;
preg_match_all($pattern,$content,$matchContent);
if(isset($matchContent1))
$temp=$matchContent1;
else
$temp=$zbp->host."zb_users/theme/$theme/images/itbulu.jpg?imageMogr2/format/webp";/固定图片地址/
{/php}

{/foreach}

2、无图随机图片
如果我们文章中无图的话,那就调用预设的随机图片,可以随机设置5个或者10个,这样丰富一些。
{php}
$temp=mt_rand(1,5);
$pattern="/<[img|IMG].?src=\'|\")[\'|\"].?[/]?>/";
$content = $article->Content;
preg_match_all($pattern,$content,$matchContent);
if(isset($matchContent1))
$temp=$matchContent1;
else
$temp=$zbp->host."zb_users/theme/$theme/style/images/random/$temp.png?imageMogr2/format/webp";
{/php}

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



在调用图片的位置放上上面的调用,我们也可以给上面图片加上样式。参考地址:https://www.itbulu.com/zbp-auto-thumbnail.html
3、函数形式调用
function zbp_thumbnail($related) {

global $zbp; 

$temp=mt_rand(1,4);

$pattern="/<[img|IMG].?src=\'|\")[\'|\"].?[/]?>/";

$content = $related->Content;

preg_match_all($pattern,$content,$matchContent);

if(isset($matchContent1)){

$thumb=$matchContent1;

}else{

$thumb=$zbp->host . "zb_users/theme/" .$zbp->theme. "/include/random/" .$temp. ".jpg?imageMogr2/format/webp";

}

return $thumb;

}


调用:
{zbp_thumbnail($article)} 

我们也需要设置随机图的预存地址,这样没有图的时候也会随机调用。
4、调用第一张图并且裁剪
定义函数:
//缩略图

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=[\'"]?(.+?)[\'"]?(?:(?: |\\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?imageMogr2/format/webp";

      }

}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;

}


这里需要用到裁剪文件timthumb.php(https://images.itbulu.com/banner/timthumb.zip),我们可以根据需要丢到上面函数提到的路径中。参考:www.yzktw.com.cn/post/386.html。
总结,以上是老蒋整理的官方和网上其他网友采用的缩略图调用ZBLOG主题模板创作时候的方法,记录下来备用。


扫描二维码,在手机上阅读!