OSDN Git Service

picture storerer updated
[pettanr/pettanr.git] / app / models / resource_picture.rb
1 class ResourcePicture < ActiveRecord::Base
2   belongs_to :artist
3   belongs_to :license
4   has_many :panel_pictures
5   belongs_to :original_picture
6   
7   before_destroy :destroy_with_file
8   
9   def destroy_with_file
10     PictureIO.resource_picture_io.delete self.filename
11     PictureIO.resource_picture_io.class.subdirs.each do |d|
12       next if d.empty?
13       PictureIO.resource_picture_io.delete(self.filename, d) if PictureIO.resource_picture_io.exist?(self.filename, d)
14     end
15   end
16   
17   def self.resize(data, dw, dh)
18     Magick::Image.from_blob(data).shift.resize(dw, dh)
19   end
20   
21   #サイズの調整(limw,limhに必ず収まるように合わせる)
22   def self.fix_size_both(limw, limh, w, h)
23     wr = if w > limw
24       limw*100/w
25     else
26       100
27     end
28     hr = if h > limh
29       limh*100/h
30     else
31       100
32     end
33     res = if wr < hr
34       #幅の方が圧縮率が高い
35       [w*wr/100, h*wr/100]
36     else
37       #高さの方が圧縮率が高い
38       [w*hr/100, h*hr/100]
39     end
40     res
41   end
42   
43   def dext
44     self.ext.downcase
45   end
46   
47   def filename
48     "#{self.id}.#{self.dext}"
49   end
50   
51   def mime_type
52     "image/#{self.dext}"
53   end
54   
55   def v(rimg)
56     rimg.flip.to_blob
57   end
58   
59   def h(rimg)
60     rimg.flop.to_blob
61   end
62   
63   def vh(rimg)
64     rimg.flip.flop.to_blob
65   end
66   
67   def url subdir = nil
68     '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
69   end
70   
71   def thumbnail(rimg)
72     tw, th = ResourcePicture.fix_size_both(64, 64, rimg.columns, rimg.rows)
73     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
74   end
75   
76   def self.update_picture(op)
77     res = op.resource_picture || ResourcePicture.new
78     res.attributes = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, 
79       :original_picture_id => op.id, :artist_id => op.artist_id, :license_id => op.license_id
80     }
81     res
82   end
83   
84   def to_gif?
85     self.dext == 'png' and self.license.no_convert == 0
86   end
87   
88   def self.png_to_gif(data)
89     begin
90       mgk = Magick::Image.from_blob(data).shift
91       mgk.format = 'gif'
92       mgk.to_blob
93       res = mgk
94     rescue
95       res = false
96     end
97     res
98   end
99   
100   def store(mgk)
101     res = false
102     if res = self.save
103       if res = self.store_picture(mgk)
104         if self.to_gif?
105           if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
106             res = self.store_picture(gifmgk)
107           else
108             res = false
109           end
110         end
111       end
112     end
113     res
114   end
115   
116   def store_picture(mgk)
117     res = false
118     PictureIO.resource_picture_io.class.subdirs.each do |d|
119       picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk)
120       res = PictureIO.resource_picture_io.put(picdata, self.filename, d)
121       break unless res
122     end
123     res
124   end
125   
126   def restore(subdir = nil)
127     PictureIO.resource_picture_io.get self.filename, subdir
128   end
129   
130 end