OSDN Git Service

picture storerer updated
[pettanr/pettanr.git] / app / models / resource_picture.rb
index 391c2db..be16276 100644 (file)
@@ -1,6 +1,6 @@
 class ResourcePicture < ActiveRecord::Base
   belongs_to :artist
-  belongs_to :lisence
+  belongs_to :license
   has_many :panel_pictures
   belongs_to :original_picture
   
@@ -69,42 +69,57 @@ class ResourcePicture < ActiveRecord::Base
   end
   
   def thumbnail(rimg)
-    tw, th = ResourcePicture.fix_size_both(80, 80, rimg.columns, rimg.rows)
+    tw, th = ResourcePicture.fix_size_both(64, 64, rimg.columns, rimg.rows)
     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
   end
   
-  def self.store(img, original_picture)
-    
-    resource_picture = ResourcePicture.new(
-      width: original_picture.width, height: original_picture.height, 
-      ext: original_picture.ext, filesize: original_picture.filesize, 
-      original_picture_id: original_picture.id
-    )
-    res = if resource_picture.save
-      if resource_picture.store(img)
-        true
-      else
-        false
-      end
-    else
-      false
+  def self.update_picture(op)
+    res = op.resource_picture || ResourcePicture.new
+    res.attributes = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, 
+      :original_picture_id => op.id, :artist_id => op.artist_id, :license_id => op.license_id
+    }
+    res
+  end
+  
+  def to_gif?
+    self.dext == 'png' and self.license.no_convert == 0
+  end
+  
+  def self.png_to_gif(data)
+    begin
+      mgk = Magick::Image.from_blob(data).shift
+      mgk.format = 'gif'
+      mgk.to_blob
+      res = mgk
+    rescue
+      res = false
     end
     res
   end
   
-  def store(img)
+  def store(mgk)
     res = false
-    bindata = img.to_blob
-#    begin
-      PictureIO.resource_picture_io.put bindata, self.filename
-      PictureIO.resource_picture_io.class.subdirs.each do |d|
-        next if d.empty?
-        PictureIO.resource_picture_io.put(self.__send__(d, img), self.filename, d)
+    if res = self.save
+      if res = self.store_picture(mgk)
+        if self.to_gif?
+          if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
+            res = self.store_picture(gifmgk)
+          else
+            res = false
+          end
+        end
       end
-      res = true
-#    rescue
-#      res = false
-#    end
+    end
+    res
+  end
+  
+  def store_picture(mgk)
+    res = false
+    PictureIO.resource_picture_io.class.subdirs.each do |d|
+      picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk)
+      res = PictureIO.resource_picture_io.put(picdata, self.filename, d)
+      break unless res
+    end
     res
   end