【备忘】Z-BlogPHP 使用随机图片 API 设置缩略图

吐槽/反馈/建议:我的咸鱼心  爱发电-@wdssmq  FeedsPub-wdssmq

简述

Z-BlogPHP 接入随机图 API 用于缩略图

代码

/**
 * 提取或者设置图片
 *
 * @param object $article
 * @param string $type   用于决定返回形式
 * @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(); // 在当前需求下此种方式并不科学。可能会被服务方限制
  }
  // 默认返回图片地址
  if ($type == 'imgurl') {
    return $imgurl;
  }
  // 传入任意其他值可返回 <img /> 标签
  $tplImg = '<img src="imgurl" alt="title">';
  return strtr($tplImg, array('imgurl' => $imgurl, 'title' => $article->Title));
}

/**
 * 可直接引用的随机图接口
 *
 * @param string $rndhash 传入一个参数用于防止图片重复
 * @return string
 */
function demoAPP_setRndImg($rndhash)
{
  return "https://picsum.photos/350/260?random={$rndhash}";
}

/**
 * 接口本身返回 json ,需要额外提取图片地址
 *
 * @return string
 */
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->setTimeOuts(10, 10, 0, 0);
  $http->send();
  // 对抓取内容进行解析
  if ($http->status == 200 && $json = json_decode($http->responseText, true)) {
    // 返回字段以实际接口为准
    $imgurl = $json['imgurl'];
  }
  return $imgurl;
}

模板内调用:

<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


爱发电

本文标题:《【备忘】Z-BlogPHP 使用随机图片 API 设置缩略图》作者:沉冰浮水
原文链接:https://www.wdssmq.com/post/20100215532.html
特别注明外均为原创,转载请注明。

分享到微信

扫描二维码

可在微信查看或分享至朋友圈。

相关文章

沉冰浮水 沉冰浮水 发布于 2010-11-05 08:44:35  
https://www.wdssmq.com
哈哈。。本来就很无聊。。
----------
冬天到了,好冷。。。
回复
lovee lovee 发布于 2010-11-04 22:51:42  
#cmt169
真无聊……
回复
沉冰浮水 沉冰浮水 发布于 2010-11-04 17:19:36  
https://www.wdssmq.com
呵呵。和。很损的。。
----------
冬天到了,好冷。。。
回复
囧啊囧 囧啊囧 发布于 2010-11-04 13:38:58  
#cmt167
一九条
。。。
回复
沉冰浮水 沉冰浮水 发布于 2010-02-22 16:15:06  
https://www.wdssmq.com
额。。有可能。。。
回复
梦舞霓裳 梦舞霓裳 发布于 2010-02-22 16:00:01  
#cmt165
你会不会被所有商场设为禁入物品
回复
发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

网站分类

搜索

控制面板

您好,欢迎到访网站!
  查看权限

本周旧文

爱发电支持者

最新留言

友情链接

  • 订阅本站的 RSS 2.0 新闻聚合
召唤伊斯特瓦尔