Brief Introduction#
Z-BlogPHP integrates random image API for thumbnails
Code#
/**
* Extract or set image
*
* @param object $article
* @param string $type Used to determine the return format
* @return string imgurl | <img />
*/
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);
// $imgurl = demoAPP_setRndImgNetwork(); // This method is not scientific under current requirements. It may be restricted by the service provider.
}
// Default return image address
if ($type == 'imgurl') {
return $imgurl;
}
// Passing any other value returns <img /> tag
$tplImg = '<img src="imgurl" alt="title">';
return strtr($tplImg, array('imgurl' => $imgurl, 'title' => $article->Title));
}
/**
* Directly callable random image interface
*
* @param string $rndhash Pass a parameter to prevent image duplication
* @return string
*/
function demoAPP_setRndImg($rndhash)
{
return "https://picsum.photos/350/260?random={$rndhash}";
}
/**
* The interface itself returns json, requires additional extraction of image address
*
* @return string
*/
function demoAPP_setRndImgNetwork()
{
global $zbp;
// Default image when failed
$imgurl = "{$zbp->host}zb_users/theme/demoAPP/var/images/no-image.jpg";
// Interface address
$url = "https://api.vvhan.com/api/acgimg?type=json";
$http = Network::Create();
$http->open('GET', $url);
// $http->setTimeOuts(10, 10, 0, 0);
$http->send();
// Parse the fetched content
if ($http->status == 200 && $json = json_decode($http->responseText, true)) {
// Return fields are subject to the actual interface
$imgurl = $json['imgurl'];
}
return $imgurl;
}
Call in template:
<a href="{$article.Url}" title="{$article.Title}">
{demoAPP_Thumbnail($article,1)}
</a>
Recommendations#
Some records about Z-BlogPHP 1.7 thumbnails_Computer Network_Floating Ice and Water:
https://www.wdssmq.com/post/20210224481.html
[Developer] Related thread on regular expressions - Developer Center - ZBlogger Technical Exchange Center: