-
Notifications
You must be signed in to change notification settings - Fork 46
/
image_ex.rb
452 lines (394 loc) · 14.6 KB
/
image_ex.rb
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# coding: utf-8
# image_plugin_ex.rb
# version 0.3
# -pv-
#
# 名称:
# 絵日記Plugin機能追加版
#
# 概要:
# 日記更新画面からの画像アップロード、サムネイル作成、本文への表示
#
# 使う場所:
# 本文
#
# 使い方:
# image( number, 'altword', thumbnail ) - 画像を表示します。
# number - 画像の番号0、1、2等
# altword - imgタグの altに入れる文字列
# thumbnail - サムネイル(小さな画像)を指定する(省略可)
#
# image_left( number, 'altword', thumbnail ) - imageにclass=leftを追加します。
# image_right( number, 'altword', thumbnail ) - imageにclass=rightを追加します。
#
# image_link( number, 'desc' ) - 画像へのリンクを生成します。
# number - 画像の番号0、1、2等
# desc - 画像の説明
#
# その他:
# tDiary version 1.5.4以降で動作します。
# tdiary.confで、
# 画像ファイルを保存するディレクトリ
# @options['image.dir']
# 画像ファイルのURL
# @options['image.url']
# 縮小画像の生成方法
# @options['image.url']
# 0 - 縮小画像を生成しない
# 1 - ImageMagickのconvertで縮小画王を生成
# 2 - netpbm群で縮小画王を生成
# を設定してください。
# また、@secure = trueな環境では動作しません。
#
# 詳しくは、
# http://shimoi.s26.xrea.com/hiki/hiki.xcg?TdiaryEnikkiEx
# をご覧下さい。
#
# ライセンスについて:
# Copyright (c) 2002 Daisuke Kato <dai@kato-agri.com>
# Copyright (c) 2002 Toshi Okada <toshi@neverland.to>
# Copyright (c) 2003 Yoshimi KURUMA <yoshimik@iris.dti.ne.jp>
#
# You can redistribute it and/or modify it under GPL2.
#
=begin Changelog
2003-05-16 Yoshimi KURUMA <yoshimik@iris.dti.ne.jp>
* method 'image' is extended to show a thumbnail.
* new method: 'image_link'.
* version 0.3
2003-05-16 Yoshimi KURUMA <yoshimik@iris.dti.ne.jp>
* modify illegal option names.
* version 0.2.
2003-05-16 Yoshimi KURUMA <yoshimik@iris.dti.ne.jp>
* support manual upload of thumbnail when useresize == 0
2003-04-27 Yoshimi KURUMA <yoshimik@iris.dti.ne.jp>
* link element is removed when no thumbnail.
2003-04-25 Yoshimi KURUMA <yoshimik@iris.dti.ne.jp>
* add JavaScript for insert plugin tag into diary.
* upload/delete form style changed.
2003-04-24 Yoshimi KURUMA <yoshimik@iris.dti.ne.jp>
* upload/delete form style changed.
2003-04-22 Yoshimi KURUMA <yoshimik@iris.dti.ne.jp>
* version 0.1 first form_proc version.
=end
@image_dir = @options && @options['image.dir'] || './images/'
@image_dir.chop! if /\/$/ =~ @image_dir
@image_url = @options && @options['image.url'] || './images/'
@image_url.chop! if /\/$/ =~ @image_url
@imageex_thumbnailsize = @options && @options['image_ex.previewsize'] || 120
@imageex_yearlydir = @options && @options['image_ex.yearlydir'] || 0
enable_js("image_ex.js")
add_body_enter_proc do |date|
@image_date = date.strftime("%Y%m%d")
@image_year = date.strftime("%Y")
""
end
def image( id, alt = "image", id2 = nil, width = nil, place="none" )
@image_date ||= @date.strftime("%Y%m%d")
@image_year ||= @date.strftime("%Y")
if @imageex_yearlydir == 1
image_url = %Q[#{@image_url}/#{@image_year}/]
image_dir = %Q[#{@image_dir}/#{@image_year}/]
else
image_url = %Q[#{@image_url}/]
image_dir = %Q[#{@image_dir}/]
end
Dir.mkdir(image_dir) unless File.directory?(image_dir)
list = imageList(@image_date, image_dir)
slist = imageList(@image_date, image_dir, "s")
if width
width_tag = %Q[width="#{h width}"]
else
width_tag = ""
end
if id2
%Q[<a href="#{h image_url}#{h list[id.to_i]}"><img class="image-ex #{h place}" src="#{h image_url}#{h list[id2.to_i]}" alt="#{h alt}"></a>]
else
if slist[id.to_i]
%Q[<a href="#{h image_url}#{h list[id.to_i]}"><img src="#{h image_url}#{h slist[id.to_i]}" alt="#{h alt}" title="#{h alt}" #{width_tag} class="image-ex #{h place}"></a>]
else
if list[id.to_i]
# %Q[<a href="#{h image_url}#{h list[id.to_i]}"><img src="#{h image_url}#{h list[id.to_i]}" alt="#{h alt}" #{width_tag} class="image-ex #{h place}"></a>]
%Q[<img src="#{h image_url}#{h list[id.to_i]}" alt="#{h alt}" title="#{h alt}" #{width_tag} class="image-ex #{h place}">]
end
end
end
end
def image_left( id, alt = "image", id2 = nil, width=nil )
image( id, alt, id2, width, "left" )
end
def image_right( id, alt = "image", id2 = nil, width=nil )
image( id, alt, id2, width, "right" )
end
def image_link( id, str )
@image_date ||= @date.strftime("%Y%m%d")
@image_year ||= @date.strftime("%Y")
if @imageex_yearlydir == 1
image_url = %Q[#{@image_url}/#{@image_year}/]
image_dir = %Q[#{@image_dir}/#{@image_year}/]
else
image_url = %Q[#{@image_url}/]
image_dir = %Q[#{@image_dir}/]
end
list = imageList(@image_date, image_dir)
%Q[<a href="#{h image_url}#{h list[id.to_i]}">#{str}</a>]
end
###
def imageList(date, image_dir='@image_dir', prefix="")
date = "#{prefix}"+date
image_path = []
Dir.foreach(image_dir){ |file|
if file =~ /(.*)\_(.*)\.(.*)/
if $1 == date
image_path[$2.to_i] = file
end
end
}
image_path
end
add_form_proc do |date|
begin
# ENV['PATH'] = nil
imageex_useresize = @options && @options['image_ex.useresize'] || 0
imageex_converttype = @options && @options['image_ex.converttype'] || 0
imageex_thresholdsize = @options && @options['image_ex.thresholdsize'] || 160
imageex_convertedwidth = @options && @options['image_ex.convertedwidth'] || 160
imageex_convertedheight = @options && @options['image_ex.convertedheight'] || 120
if imageex_useresize == 1 || imageex_useresize ==2
begin
require 'image_size.rb'
rescue LoadError
imageex_useresize = 0
end
end
if imageex_useresize == 1
def resize_image(orig, new, width, height, imageex_convertedwidth, imageex_convertedheight, orig_type, new_type)
imageex_convertpath = @options && @options['image_ex.convertpath'] || "convert"
imageex_convertpath
if width > height
imageex_convertedsize = %Q[#{imageex_convertedwidth}x#{imageex_convertedheight}]
imageex_convertedsize
else
imageex_convertedsize = %Q[#{imageex_convertedheight}x#{imageex_convertedwidth}]
imageex_convertedsize
end
system(imageex_convertpath , "-auto-orient", "-geometry", imageex_convertedsize , orig, new)
if FileTest::size?( new ) == 0
File::delete( new )
end
end
elsif imageex_useresize == 2
def resize_image(orig, new, width, height, imageex_convertedwidth, imageex_convertedheight, orig_type, new_type)
pnmscale = @options && @options['image_ex.pnmscalepath'] || "pnmscale"
jpegtopnm = @options && @options['image_ex.jpegtopnmpath'] || "jpegtopnm"
pnmtojpeg = @options && @options['image_ex.pnmtojpegpath'] || "pnmtojpeg"
pngtopnm = @options && @options['image_ex.pngtopnmpath'] || "pngtopnm"
pnmtopng = @options && @options['image_ex.pnmtopngpath'] || "pnmtopng"
giftopnm = @options && @options['image_ex.giftopnmpath'] || "giftopnm"
tifftopnm = @options && @options['image_ex.tifftopnmpath'] || "tifftopnm"
bmptopnm = @options && @options['image_ex.bmptopnmpath'] || "bmptopnm"
downtype = orig_type.downcase
topnm = eval("#{downtype}topnm")
if new_type == "jpg"
pnmto = pnmtojpeg
elsif new_type == "png"
pnmto = pnmtopng
end
if width > height
imageex_convertedsize ="#{imageex_convertedwidth}"
else
imageex_convertedsize ="#{imageex_convertedheight}"
end
com_line =%Q[#{topnm} #{orig} | #{pnmscale} --width #{imageex_convertedsize} | #{pnmto} > #{new}]
system( com_line )
if FileTest::size?( new ) == 0
File::delete( new )
end
end
end
def dayimagelist( image_dir, image_date, prefix="")
image_path = []
Dir.foreach(image_dir){ |file|
if file=~ /(.*)\_(.*)\.(.*)/
if $1 == "#{prefix}" + image_date.to_s
image_path[$2.to_i] = file
end
end
}
return image_path
end
if @cgi.params['plugin_image_add'][0] && @cgi.params['plugin_image_file'][0].original_filename != ''
image_dir = @cgi.params['plugin_image_dir'][0].read
image_filename = ''
image_extension = ''
image_date = date.strftime("%Y%m%d")
image_filename = @cgi.params['plugin_image_file'][0].original_filename
if image_filename =~ /(\.jpg|\.jpeg|\.gif|\.png)\z/i
image_extension = $1
image_name = dayimagelist(image_dir, image_date)
image_file = image_dir+image_date+"_"+image_name.length.to_s+image_extension.downcase
File::umask( 022 )
File::open( image_file, "wb" ) {|f|
f.print @cgi.params['plugin_image_file'][0].read
}
end
if imageex_useresize == 1 or imageex_useresize == 2
open(image_file,"rb") do |fh|
img = ImageSize.new(fh.read)
begin
# old image_size.rb
width = img.get_width
height = img.get_height
orig_type = img.get_type
rescue NoMethodError
# image_size gem
width = img.width
height = img.height
orig_type = (img.format || "OTHER").to_s.upcase
end
if imageex_converttype == 0
new_type = "jpg"
elsif imageex_converttype == 1
new_type = "png"
end
if width
if width > imageex_thresholdsize or height > imageex_thresholdsize
small_image_file = %Q[#{image_dir}s#{image_date}_#{image_name.length.to_s}.#{new_type}]
resize_image(image_file, small_image_file, width, height, imageex_convertedwidth, imageex_convertedheight, orig_type, new_type)
end
end
end
end
elsif @cgi.params['plugin_image_thumbnail'][0] && @cgi.params['plugin_image_file'][0].original_filename != ''
image_dir = @cgi.params['plugin_image_dir'][0].read
image_filename = ''
image_extension = ''
image_date = date.strftime("%Y%m%d")
image_filename = @cgi.params['plugin_image_file'][0].original_filename
if image_filename =~ /(\.jpg|\.jpeg|\.gif|\.png)\z/i
image_extension = $1
image_name = @cgi.params['plugin_image_name'][0].read
image_file=image_dir+"s"+image_name+image_extension.downcase
File::umask( 022 )
File::open( image_file, "wb" ) {|f|
f.print @cgi.params['plugin_image_file'][0].read
}
end
elsif @cgi.params['plugin_image_del'][0]
image_dir = @cgi.params['plugin_image_dir'][0]
image_date = date.strftime("%Y%m%d")
image_name = dayimagelist( image_dir, image_date)
image_name2= dayimagelist( image_dir, image_date, "s")
@cgi.params['plugin_image_id'].each do |id|
if image_name[id.to_i]
image_file=image_dir+image_name[id.to_i]
if File::exist?(image_file)
File::delete(image_file)
end
end
if image_name2[id.to_i]
image_file2=image_dir+image_name2[id.to_i]
if File::exist?(image_file2)
File::delete(image_file2)
end
end
end
end
rescue Exception
puts "Content-Type: text/plain\n\n"
puts "#$! (#{$!.type})"
puts ""
puts $@.join( "\n" )
end
if @imageex_yearlydir == 1
image_dir = %Q[#{@image_dir}/#{@date.year}/]
else
image_dir = %Q[#{@image_dir}/]
end
if @imageex_yearlydir == 1
image_url = %Q[#{@image_url}/#{@date.year}/]
else
image_url = %Q[#{@image_url}/]
end
Dir.mkdir(image_dir) unless File.directory?(image_dir)
n_image = imageList(@date.strftime("%Y%m%d"), image_dir).length
list = imageList(@date.strftime("%Y%m%d"), image_dir)
slist = imageList(@date.strftime("%Y%m%d"), image_dir, "s")
pretable=""
posttable=""
if n_image > 0
pretable<< %Q[<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">]
posttable << %Q[</TR></TABLE>]
end
if @conf.respond_to?(:style) and @conf.style =~ /(Wiki|GFM)$/i
image_plugin_tag1 = "{{"
image_plugin_tag2 = "}}"
elsif @conf.respond_to?(:style) and @conf.style =~ /RD$/i
image_plugin_tag1 = "((%"
image_plugin_tag2 = "%))"
else
image_plugin_tag1 = "<%="
image_plugin_tag2 = "%>"
end
r = ''
r << %Q[
<script type="text/javascript">
<!--
var elem=null;
function ins(val){
elem.value+=val
}
$(document).ready(function(){
$("textarea").focus(function() { elem = this; });
});
//-->
</script>
]
i = ''
i << "<tr>"
nt=0
id=0
while id < n_image do
thumbnail_tag = ''
if slist[id.to_i]
src_tag = %Q[src="#{h image_url}#{h slist[id.to_i]}"]
alt_tag = %Q[alt="#{h slist[id.to_i]}"]
else
src_tag = %Q[src="#{h image_url}#{h list[id.to_i]}"]
alt_tag = %Q[alt="#{h list[id.to_i]}"]
thumbnail_tag = %Q[<form class="update" method="post" enctype="multipart/form-data" action="#{h @update}">#{csrf_protection}<input type="hidden" name="plugin_image_name" value="#{date.strftime( '%Y%m%d' )}_#{h id}"><input type="hidden" name="plugin_image_dir" value="#{h image_dir}"><input type="hidden" name="plugin_image_thumbnail" value="true"><input type="hidden" name="date" value="#{date.strftime( '%Y%m%d' )}"><input type="file" name="plugin_image_file"><input type="submit" name="plugin" value="サムネイル"></form>] if imageex_useresize == 0
end
ptag = "#{image_plugin_tag1}image #{id}, '画像の説明'#{image_plugin_tag2}"
i<< %Q[<td><table border="1" cellpadding="1" cellspacing="1"><tr><td style="text-align:center"><input type="button" onclick="ins("#{ptag}")" value="本文に追加"></td></tr><tr><td style="text-align:center">#{image_plugin_tag1}image #{h id},'title#{h id}'#{image_plugin_tag2}</td></tr><tr><td width="#{@imageex_thumbnailsize * 1.5}" height="#{@imageex_thumbnailsize * 1.3}" style="text-align:center">
<img class="form" #{src_tag} #{alt_tag} height="#{@imageex_thumbnailsize}" ></tr><tr><td>#{thumbnail_tag}<form class="update" method="post" action="#{h @update}">#{csrf_protection}<input type="hidden" name="plugin_image_del" value="true"><input type="hidden" name="date" value="#{date.strftime( '%Y%m%d' )}"><input type="hidden" name="plugin_image_id" value="#{h id}"><input type="submit" name="plugin" value="画像を削除"><input type="hidden" name="plugin_image_dir" value="#{h image_dir}"></form>
</tr></table></td>] if slist[id.to_i] || list[id.to_i]
nt += 1 if slist[id.to_i] || list[id.to_i]
if nt > 0 and nt%2 == 0
i<< %Q[</TR></TABLE><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0"><TR><TD>]
end
id +=1
end
if n_image > 0
r << %Q[<div class="form">
<div class="caption">
絵日記(一覧・削除)
</div>
#{pretable}
#{i}
#{posttable}
</div>]
end
r << %Q[<div class="form">
<div class="caption">
絵日記(追加)
</div>
<form class="update" method="post" enctype="multipart/form-data" action="#{h @update}">
#{csrf_protection}
<input type="hidden" name="plugin_image_dir" value="#{h image_dir}">
<input type="hidden" name="plugin_image_add" value="true">
<input type="file" name="plugin_image_file">
<input type="hidden" name="date" value="#{date.strftime( '%Y%m%d' )}">
<input type="submit" name="plugin" value="画像の追加">
</form></div>]
end
# vim: set ts=3 sw=3 noexpandtab :