#This is the sample appliocation code ,Please change as per your requirements.
======Image Uploading instruction=========
1.routes.rb
#this is for image display
map.with_options :controller => ‘images’ do |image|
image.show ‘images/show/:size/:id/:default_image’, :action => ’show’, :id => 0, :default_image => nil, :requirements => {:size => /(tiny|small|medium|original)/}
end
2. model
-image.rb file
require ‘RMagick’
class Image < ActiveRecord::Base
def thumb=(input_data)
if input_data != “”
self.name = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.image_original = input_data.read
img = Magick::Image.from_blob(self.image_original)
original_height = img[0].rows
original_width = img[0].columns
self.height = original_height
self.width = original_width
tiny_height = 32
tiny_width = 32
small_height = 85
small_width = 85
medium_height = 180
medium_width = 180
# resize the photo keeping aspect ratio
if original_height > original_width
tiny_width = original_width * tiny_height / original_height
small_width = original_width * small_height / original_height
medium_width = original_width * medium_height / original_height
else
tiny_height = original_height * tiny_width / original_width
small_height = original_height * small_width / original_width
medium_height = original_height * medium_width / original_width
end
self.image_tiny = img[0].thumbnail(tiny_width, tiny_height).to_blob
self.image_small = img[0].thumbnail(small_width, small_height).to_blob
self.image_medium = img[0].thumbnail(medium_width, medium_height).to_blob
end
end
end
3. controller
-images_controller.rb file
#this is for image display
def show
# verify valid size parameter here
valid_display_size = ["tiny", "small", "medium", "original"]
display_size = (valid_display_size.include?params[:size])?params[:size]:”medium”
#display_size = params[:size]?params[:size]:”medium”
image_id = params[:id]
default_image = params[:default_image]?params[:default_image]:”no_photo_#{display_size}”
if image_id != 0
begin
@image = Image.find(image_id)
if !(eval(“@image.image_#{display_size}”)).blank?
send_data(eval(“@image.image_#{display_size}”), :type => @image.content_type,
:filename => @image.name,
:disposition => ‘inline’)
return
end
rescue
end
# # show default image here
# #send_image(“#{RAILS_ROOT}/public/images/#{default_image}”)
# @no_image = File.open(“#{RAILS_ROOT}/public/images/#{default_image}.png”,”r”)
# send_data(@no_image.read, :type => “image/png”,
# :filename => “#{default_image}.png”,
# :disposition => ‘inline’)
end
#else
# show default image here
#send_image(“#{RAILS_ROOT}/public/images/#{default_image}”)
@no_image = File.open(“#{RAILS_ROOT}/public/images/#{default_image}.png”,”r”)
send_data(@no_image.read, :type => “image/png”,
:filename => “#{default_image}.png”,
:disposition => ‘inline’)
#end
end
#save iamge
def saveimage
if params[:image][:title].blank?
flash[:notice] = “Title cannot be left blank.”
elsif params[:image][:thumb].blank?
flash[:notice] = “Please select Image.”
else
@picture = Image.new(params[:image])
if @picture.save
flash[:notice] = “Item was successfully created.”
redirect_to :action => “index”
return
else
flash[:notice] = “Error occured during insertion.”
end
end
redirect_to :action => “addimage”
end
#update iamge
def updateimage
if params[:image][:title].blank?
flash[:notice] = “Title cannot be left blank.”
else
@items = Image.find(params[:id])
if @items.update_attributes(params[:image])
flash[:notice] = ‘Photo was successfully updated.’
redirect_to :action => “index”
return
end
end
redirect_to :action => “editimage”,:id=>params[:id]
end
4. view
-add_image.rhtml=
<%= form_tag ({:action => ’saveimage’},:multipart => true) %>
<table>
<tr><td>Title</td>
<td><%= text_field (“image”,”title”,:size=>”30″) %><span id=”red”> *</span>
</td></tr>
<tr><td>Description</td>
<td><%= text_area (“image”,”description”,:cols=>”28″,:rows=>4)%>
</td></tr>
<tr><td>Upload Image</td><td>
<%= file_field ‘image’, ‘thumb’, :size => “20″ %><span id=”red”> *</span>
</td></tr>
</table></form>
#show image/display
<% @items_all = Image.find(:all)
for @items in @items_all %>
<%= image_tag(“/images/show/small/#{@items.id ? @items.id : “0″}/no_photo_#{“”}_small”, :alt => “Image not available”) %>
<%= image_tag(“/images/show/medium/#{@items.id ? @items.id : “0″}/no_photo_#{“”}_medium”, :alt => “Image not available”) %>
<% end %>
5. database structure
image table===
id=int
title=varchar
name=varchar
content_type=varchar
image_tiny=midiumblob
image_small=midiumblob
image_midium==midiumblob
image_original==midiumblob
height=bigint(11)
width=bigint(11)