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
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
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
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
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
img.resize!(cols, rows)
end
end
- data = image_data(image)
+ data = image_data(image, format)
run_gc
data
end
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