forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
105 lines (94 loc) · 2.73 KB
/
Plugin.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
*
* 随机图片挂件widget
*
* @package Typecho-RandomThumbnail
* @author LittleJake
* @version 2.0.0
* @link https://blog.littlejake.net
*/
class RandomThumbnail_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法
*
* @return void
*/
public static function activate(){}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @access public
* @return void
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){
$url = new Typecho_Widget_Helper_Form_Element_Textarea('url', NULL, NULL, _t('图片地址'), _t('输入图片地址,一行一条'));
$html = <<<EOF
<div style="width: fit-content; height: 300px; overflow: hidden; border-radius: 10px; max-height: 100%; max-width: 100%; margin:5% auto;">
<img src="{img_src}" alt="head-img" class="" style="">
</div>
EOF;
$template = new Typecho_Widget_Helper_Form_Element_Textarea(
'template',
NULL,
$html,
_t('图片显示自定义模板'),
_t('可用变量参考:<a href="https://github.com/LittleJake/Typecho-RandomThumbnail/blob/master/README.md" target="_blank">README.md</a>')
);
$form->addInput($url);
$form->addInput($template);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
*
* 获取缩略图
*
* @author LittleJake
* @param int $seed 随机数
* @return bool
*/
public static function getThumbnail($seed = 0)
{
try{
$url = Typecho_Widget::widget('Widget_Options')->plugin('RandomThumbnail')->url;
$urls = explode("\r\n",$url);
if(sizeof($urls) == 0)
return false;
$seed = $seed>0?$seed:rand(0,9999);
$num = sizeof($urls);
$index = $seed % $num;
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);
}
}