root/platform/tdiary/plugin/image_ex.rb

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

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

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