OSDN Git Service

携帯でアクセスした時に元がPNG画像の場合Docomo端末で正常に表示できない問題を修正`
authorTaro Matsuzawa <tmatsuzawa@kbmj.com>
Tue, 26 Oct 2010 09:51:31 +0000 (18:51 +0900)
committerTaro Matsuzawa <tmatsuzawa@kbmj.com>
Tue, 26 Oct 2010 09:51:31 +0000 (18:51 +0900)
app/controllers/image_resource_controller.rb
app/helpers/base_helper.rb
app/models/image_resource.rb

index 0c9ac75..cd62cd7 100644 (file)
@@ -11,12 +11,24 @@ class ImageResourceController < BaseController
     end
     raise ActiveRecord::RecordNotFound unless @res
     if request.mobile?
+      format = nil
+      if request.mobile.instance_of?(Jpmobile::Mobile::Docomo)
+        format = :gif
+      elsif request.mobile.instance_of?(Jpmobile::Mobile::Au)
+        format = :gif
+      elsif request.mobile.instance_of?(Jpmobile::Mobile::Softbank)
+        format = :png
+      else
+        format = :jpeg
+      end
       if params[:width].present? || params[:height].present?
         width, height = [params[:width].to_i, params[:height].to_i]
       else
         width, height = request.mobile.display.width, request.mobile.display.height
       end
-      send_file @res, @res.scaled_image(width, height)
+      send_file @res, @res.scaled_image(width, height, format), format
+    elsif params[:format]
+      send_file @res, @res.view_with_format(params[:format])
     else
       send_file @res, @res.view
     end
@@ -27,13 +39,23 @@ class ImageResourceController < BaseController
 
   private
   
-  def send_file(res, data)
+  def send_file(res, data, format = nil)
     raise ActiveRecord::RecordNotFound unless res
     raise ActiveRecord::RecordNotFound unless data
     content_type = res.content_type
     if request.mobile?
       content_type.gsub!(/pjpeg/, "jpeg")
     end
+    if format
+      case format
+      when :gif
+        content_type = "image/gif"
+      when :png
+        content_type = "image/png"
+      when :jpeg
+        content_type = "image/jpeg"
+      end
+    end
     send_data(data, :type => content_type, :disposition => 'inline')
   end
 end
index 3529567..90e4bb4 100644 (file)
@@ -105,7 +105,21 @@ module BaseHelper
 
   def view_resource_id(resource_id, options = {})
     if resource_id && resource_id != 0
-      image_tag url_for(:controller => "/image_resource", :action => "show", :id => resource_id, :height=>options[:height], :width=>options[:width]), options
+      if request.mobile?
+        format = nil
+        if request.mobile.instance_of?(Jpmobile::Mobile::Docomo)
+          format = :gif
+        elsif request.mobile.instance_of?(Jpmobile::Mobile::Au)
+          format = :gif
+        elsif request.mobile.instance_of?(Jpmobile::Mobile::Softbank)
+          format = :png
+        else
+          format = :jpg
+        end
+        image_tag url_for(:controller => "/image_resource", :action => "show", :id => resource_id, :format => format, :height => options[:height], :width => options[:width]), options
+      else
+        image_tag url_for(:controller => "/image_resource", :action => "show", :id => resource_id, :height=>options[:height], :width=>options[:width]), options
+      end
     elsif resource_id == 0
       ""
     else
index 81da753..f8319b4 100644 (file)
@@ -10,6 +10,13 @@ class ImageResource < ActiveRecord::Base
     resource_data.content
   end
 
+  def view_with_format(format)
+    image = read_image(content_data)
+    data = image_data(image, format)
+    run_gc
+    data
+  end
+
   def content_data
     resource_data = ResourceData.find_by_resource_id(self.id)
     resource_data.content
@@ -24,7 +31,7 @@ class ImageResource < ActiveRecord::Base
     resource
   end
 
-  def scaled_image(width, height)
+  def scaled_image(width, height, format)
     image = read_image(content_data)
     image.change_geometry("#{width}x#{height}") do |cols,rows,img|
       rows = 1 if rows == 0
@@ -36,7 +43,7 @@ class ImageResource < ActiveRecord::Base
         img.resize!(cols, rows)
       end
     end
-    data = image_data(image)
+    data = image_data(image, format)
     run_gc
     data
   end
@@ -59,15 +66,24 @@ class ImageResource < ActiveRecord::Base
     end
   end
 
-  def image_data(image)
+  def image_data(image, format = nil)
     temp_filename do |filename|
-      image.write(filename)
+      if format
+        logger.debug "image.write " + get_format(format) + filename
+        image.write(get_format(format) + filename)
+      else
+        image.write(filename)
+      end
       File.open(filename, "rb") do |file|
         file.read
       end
     end
   end
 
+  def get_format(format)
+    format.to_s + ":"
+  end
+
   def run_gc
     fDisabled = GC.enable
     GC.start