简述
Z-BlogPHP 接入随机图 API 用于缩略图
代码
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
function demoAPP_Thumbnail($article, $type = 'imgurl') {
$matches = null; preg_match_all("/<img[^>]*src=\"([^\"]+)\"[^>]*>/i", $article->Content, $matches); if (isset($matches[1]) && count($matches[1]) > 0) { $imgurl = $matches[1][0]; } else { $imgurl = demoAPP_setRndImg($article->ID); } if ($type == 'imgurl') { return $imgurl; } $tplImg = '<img src="imgurl" alt="title">'; return strtr($tplImg, array('imgurl' => $imgurl, 'title' => $article->Title)); }
function demoAPP_setRndImg($rndhash) { return "https://picsum.photos/350/260?random={$rndhash}"; }
function demoAPP_setRndImgNetwork() { global $zbp; $imgurl = "{$zbp->host}zb_users/theme/demoAPP/var/images/no-image.jpg"; $url = "https://api.vvhan.com/api/acgimg?type=json"; $http = Network::Create(); $http->open('GET', $url); $http->send(); if ($http->status == 200 && $json = json_decode($http->responseText, true)) { $imgurl = $json['imgurl']; } return $imgurl; }
|
模板内调用:
1 2 3
| <a href="{$article.Url}" title="{$article.Title}"> {demoAPP_Thumbnail($article,1)} </a>
|
推荐
关于 Z-BlogPHP 1.7 缩略图的一些记录_电脑网络_沉冰浮水:
https://www.wdssmq.com/post/20210224481.html
[开发者]正则表达式相关专贴-开发者中心-ZBlogger 技术交流中心:
https://bbs.zblogcn.com/thread-101713.html