From 4733b2959c3f81bb502a3c505ade2af8c7fae8b9 Mon Sep 17 00:00:00 2001 From: Taro Matsuzawa Date: Tue, 26 Oct 2010 18:51:31 +0900 Subject: [PATCH] =?utf8?q?=E6=90=BA=E5=B8=AF=E3=81=A7=E3=82=A2=E3=82=AF?= =?utf8?q?=E3=82=BB=E3=82=B9=E3=81=97=E3=81=9F=E6=99=82=E3=81=AB=E5=85=83?= =?utf8?q?=E3=81=8CPNG=E7=94=BB=E5=83=8F=E3=81=AE=E5=A0=B4=E5=90=88Docomo?= =?utf8?q?=E7=AB=AF=E6=9C=AB=E3=81=A7=E6=AD=A3=E5=B8=B8=E3=81=AB=E8=A1=A8?= =?utf8?q?=E7=A4=BA=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C?= =?utf8?q?=E3=82=92=E4=BF=AE=E6=AD=A3`?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app/controllers/image_resource_controller.rb | 26 ++++++++++++++++++++++++-- app/helpers/base_helper.rb | 16 +++++++++++++++- app/models/image_resource.rb | 24 ++++++++++++++++++++---- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/app/controllers/image_resource_controller.rb b/app/controllers/image_resource_controller.rb index 0c9ac75..cd62cd7 100644 --- a/app/controllers/image_resource_controller.rb +++ b/app/controllers/image_resource_controller.rb @@ -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 diff --git a/app/helpers/base_helper.rb b/app/helpers/base_helper.rb index 3529567..90e4bb4 100644 --- a/app/helpers/base_helper.rb +++ b/app/helpers/base_helper.rb @@ -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 diff --git a/app/models/image_resource.rb b/app/models/image_resource.rb index 81da753..f8319b4 100644 --- a/app/models/image_resource.rb +++ b/app/models/image_resource.rb @@ -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 -- 2.11.0