root/platform/tdiary/plugin/tdiarytimes.rb

Revision 5057, 5.0 kB (checked in by hsbt, 11 months ago)

platform/tdiary/plugin: changed file encoding to UTF-8

Line 
1# tdiarytimes.rb $Revision: 1.2 $
2#
3# Copyright (c) 2003 neuichi <neuichi@nmnl.jp>
4# Distributed under the GPL
5#
6# プラグイン配布ページ
7# http://nmnl.jp/hiki/software/?tDiary+%3A%3A+Plugin
8#
9# 動作条件:
10# ruby-gdが使える環境が必要です。
11#
12# 使い方:
13# このプラグインをプラグインディレクトリに入れ、
14# index.rbと同じディレクトリに、tdiarytimes.pngという名前の
15# サーバが書き込み権限を持っているファイルを作ります。
16#       これで日記に書き込みするごとに、tdiarytimes.pngに
17#       画像を書き込みます。
18#
19# 日記上からこのpngファイルを呼び出すには、
20# tDiray上からプラグインとして
21# <%=tdiarytimes%>
22# として呼び出します。
23# 引数としてimgタグのaltの文字列を指定することも出来ます。
24# <%=tdiarytimes '文字列'%>
25#
26# また、tdiary.confに以下のオプションを書き込むことにより、
27# カスタマイズをすることが出来ます。
28#
29# @options['tdiarytimes.width'] = 400
30# 四角の横幅。デフォルト値400。
31# 実際に出力される画像サイズは、これに+10したサイズ。
32#
33# @options['tdiarytimes.height'] = 20
34# 四角の縦幅。デフォルト値20。
35# 実際に出力される画像サイズは、これに+16したサイズ。
36#
37# @options['tdiarytimes.file'] = 'tdiarytimes.png'
38# 出力する画像ファイル名。デフォルトは'tdiarytimes.png'
39#
40# @options['tdiarytimes.fillcolor'] = '#444444'
41# 四角の色。デフォルトは'#444444'
42#
43# @options['tdiarytimes.linecolor'] = '#ffffff'
44# 縦棒の色。デフォルトは'#ffffff'
45#
46# @options['tdiarytimes.textcolor'] = '#444444'
47# 文字色。デフォルトは'#444444'
48#
49# @options['tdiarytimes.text'] = 'T D I A R Y T I M E S'
50# 出力する文字。デフォルトは'T D I A R Y T I M E S'。なお半角英数字のみ対応。
51#
52# @options['tdiarytimes.day'] = 30
53# ログを保存する最大日数。デフォルトは30。
54# この場合、30日以上経ったデータは消去され、縦棒として描画されなくなる。
55#
56
57require 'GD'
58
59if /^(append|replace)$/ =~ @mode then
60
61        #初期設定
62        width = @options['tdiarytimes.width'] || 400
63        height = @options['tdiarytimes.height'] || 20
64        file = @options['tdiarytimes.file'] || 'tdiarytimes.png'
65        fillcolor = @options['tdiarytimes.fillcolor'] || '#444444'
66        linecolor = @options['tdiarytimes.linecolor'] || '#ffffff'
67        textcolor = @options['tdiarytimes.textcolor'] || '#444444'
68        text = @options['tdiarytimes.text'] || 'T D I A R Y T I M E S'
69        day = @options['tdiarytimes.day'] || 30
70       
71        cache = "#{@cache_path}/tdiarytimes"
72        Dir::mkdir( cache ) unless File::directory?( cache )
73
74        image = GD::Image.new(width + 10,height + 16)
75        transcolor = image.colorAllocate("#fffffe")
76        image.transparent(transcolor)
77        image.interlace = TRUE
78        fillcolor = image.colorAllocate(fillcolor)
79        linecolor = image.colorAllocate(linecolor)
80        textcolor = image.colorAllocate(textcolor)
81       
82        #帯の描画
83        image.filledRectangle(5,8,width + 4,height + 7,fillcolor)
84
85        #時間挿入
86        if width >= 160
87                hour = 2
88                hour_w = width / 12.0
89                image.string(GD::Font::TinyFont, 2, height + 8, "0", textcolor)
90                11.times {
91                        image.string(GD::Font::TinyFont, (hour_w * hour/2).to_i , height + 8, hour.to_s, textcolor)
92                        hour += 2
93                }
94                image.string(GD::Font::TinyFont, width + 2, height + 8, "0", textcolor)
95        else
96                hour = 0
97                hour_w = width / 6.0
98                6.times {
99                        image.string(GD::Font::TinyFont, (hour_w * hour/4).to_i + 4, height + 8, hour.to_s, textcolor)
100                        hour += 4
101                }
102                image.string(GD::Font::TinyFont, width + 2, height + 8, "0", textcolor)
103        end
104
105        #現在時刻の保存,読み込み
106        begin
107                io = open("#{cache}/tdiarytimes.dat","r")
108            ary_times =  Marshal.load(io)
109          io.close
110        rescue
111                ary_times = []
112        end
113
114        ary_times << Time.now.to_f
115        ary_times_new = []
116
117        while ary_times.size != 0
118                time = ary_times.shift
119                time_now = Time.now.to_f.to_i
120                ary_times_new << time.to_i if (86400 * day) > (time_now - time).to_i
121        end
122
123        ary_times = ary_times_new
124
125        io = open("#{cache}/tdiarytimes.dat","w")
126          Marshal.dump(ary_times,io)
127        io.close
128
129
130        #時間軸の挿入
131        while ary_times.size != 0
132                time = Time.at(ary_times.shift)
133                time_w = ((time.to_a[2] * 60 + time.to_a[1]) / 1440.0 * width).to_i
134                image.line(time_w + 5, 8 ,time_w + 5,height + 7, linecolor)
135        end
136
137        #文字の挿入
138        image.string(GD::Font::TinyFont, 5, 0, text, textcolor)
139
140        pngfile = open(file, 'w')
141                image.png(pngfile)
142        pngfile.close
143       
144end
145
146def tdiarytimes(alt = nil)
147        width = @options['tdiarytimes.width'].to_i || 400
148        width += 10
149       
150        height = @options['tdiarytimes.height'].to_i || 20
151        height += 16
152       
153        file = @options['tdiarytimes.file'] || 'tdiarytimes.png'
154        text = @options['tdiarytimes.text'] || 'T D I A R Y T I M E S'
155
156        result = ""
157       
158        if alt
159                result << %Q|<img src="#{h file}" alt="#{h alt}" width="#{width}" height="#{height}" class="tdiarytimes">|
160        else
161                result << %Q|<img src="#{h file}" alt="#{h text}" width="#{width}" height="#{height}" class="tdiarytimes">|
162        end
163
164        result
165
166end
Note: See TracBrowser for help on using the browser.