From 31136a63210ea585c004c1f04b49bc7f8419d7b2 Mon Sep 17 00:00:00 2001 From: LittleJake <465917717@qq.com> Date: Wed, 17 Feb 2021 13:04:21 +0800 Subject: [PATCH] update: RandomThumbnail Plugin --- RandomThumbnail/Plugin.php | 57 +++++++++++++++++++------------------- RandomThumbnail/README.md | 21 ++++++++------ 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/RandomThumbnail/Plugin.php b/RandomThumbnail/Plugin.php index 64158c90..f77b00c9 100644 --- a/RandomThumbnail/Plugin.php +++ b/RandomThumbnail/Plugin.php @@ -18,10 +18,7 @@ class RandomThumbnail_Plugin implements Typecho_Plugin_Interface */ public static function activate() { - Typecho_Plugin::factory('Widget_Archive')->beforeRender = array( - 'RandomThumbnail_Plugin', - ); - + Typecho_Plugin::factory('Widget_Archive')->beforeRender = array('RandomThumbnail_Plugin',); } /** @@ -40,15 +37,21 @@ public static function deactivate(){} * @return void */ public static function config(Typecho_Widget_Helper_Form $form){ - $url = new Typecho_Widget_Helper_Form_Element_Textarea('url', NULL, NULL, _t('缩略图地址'), _t('输入图片地址,一行一条')); - $lazyload = new Typecho_Widget_Helper_Form_Element_Radio('lazyload',array('0' => _t('否'),'1' => _t('是')),'0', _t('开启Lazyload')); - $lazyload_tag = new Typecho_Widget_Helper_Form_Element_Text('lazyload_tag', NULL, NULL, _t('图片URL标签'), _t('输入图片图片URL标签')); - $lazyload_url = new Typecho_Widget_Helper_Form_Element_Text('lazyload_url', NULL, NULL, _t('替换图片地址'), _t('输入图片地址')); + $url = new Typecho_Widget_Helper_Form_Element_Textarea('url', NULL, NULL, _t('图片地址'), _t('输入图片地址,一行一条')); + $template = new Typecho_Widget_Helper_Form_Element_Textarea( + 'template', + NULL, + << + head-img + +EOF, + _t('图片显示自定义模板'), + _t('可用变量参考:README.md') + ); $form->addInput($url); - $form->addInput($lazyload); - $form->addInput($lazyload_tag); - $form->addInput($lazyload_url); + $form->addInput($template); } /** @@ -66,21 +69,13 @@ public static function personalConfig(Typecho_Widget_Helper_Form $form){} * * @author LittleJake * @param int $seed 随机数 - * @param string $height 图片高度 - * @param string $width 图片宽度 - * @param string $class 自定义class - * @param string $style 自定义样式 * @return bool */ - public static function getThumbnail($seed = 0,$height = "fit-content", $width = "fit-content", $class = '', $style = '') + public static function getThumbnail($seed = 0) { try{ $url = Typecho_Widget::widget('Widget_Options')->plugin('RandomThumbnail')->url; - $lazyload = Typecho_Widget::widget('Widget_Options')->plugin('RandomThumbnail')->lazyload; - $lazyload_tag = Typecho_Widget::widget('Widget_Options')->plugin('RandomThumbnail')->lazyload_tag; - $lazyload_url = Typecho_Widget::widget('Widget_Options')->plugin('RandomThumbnail')->lazyload_url; - $urls = explode("\r\n",$url); if(sizeof($urls) == 0) @@ -88,21 +83,25 @@ public static function getThumbnail($seed = 0,$height = "fit-content", $width = $seed = $seed>0?$seed:rand(0,9999); $num = sizeof($urls); - $index = $seed % $num; - if($lazyload == 1) - echo "
-head-img-$seed -
"; - else - echo "
-head-img-$seed -
"; + echo self::format($urls[$index]); + return true; } catch (\Exception $e){ return false; } + } + /** + * 用于处理模板数据 + * + * @param $url + * @return string|string[] + * @throws Typecho_Exception + */ + public static function format($url){ + return str_replace('{img_src}',$url, Typecho_Widget::widget('Widget_Options') + ->plugin('RandomThumbnail')->template); } } diff --git a/RandomThumbnail/README.md b/RandomThumbnail/README.md index 59783ee1..594fce17 100644 --- a/RandomThumbnail/README.md +++ b/RandomThumbnail/README.md @@ -4,25 +4,28 @@ 根据后台设置链接随机生成图片。 - + +支持自定义模板。 ## 安装方法 -1. `git clone`或zip下载,将 Typecho-RandomThumbnail 文件夹放入`网站目录/usr/plugins`文件夹内,文件夹权限0755,插件文件0644。 +1. `git clone`或zip下载,将 Typecho-RandomThumbnail 中`RandomThumbnail`文件夹放入`网站目录/usr/plugins`文件夹内,文件夹权限0755,插件文件0644。 2. 打开Typecho后台激活插件 3. 添加图片链接 4. 在挂件位置插入 ```php - + ``` - +## 参数说明 +### 调用函数参数 | 参数 | 参数名 | 参数类型 | 备注 | | ------- | ----------- | -------- | ---- | | $seed | 随机数 | int | 可选 | -| $height | 图片高度 | int | 可选 | -| $width | 图片宽度 | int | 可选 | -| $class | 自定义class | string | 可选 | -| $style | 自定义样式 | string | 可选 | + +### 设置模板参数 +| 参数 | 参数名 | 参数类型 | 备注 | +| ------- | ---------- | -------- | ---- | +| img_url | 图片地址 | String | - | ## Demo @@ -36,4 +39,4 @@ Typecho ## 开源协议 -基于Apache2.0 \ No newline at end of file +Apache2.0 \ No newline at end of file