Its all about Ruby On Rails
Plugin: file_column
This is straight from the “file_column website”:http://www.kanthak.net/opensource/file_column/:
Just make the “image” column ready for handling uploaded files…
class Entry < ActiveRecord::Base
file_column :image
end … generate file fields *that keep uploaded images during form redisplays to your view…
<%= file_column_field "entry", "image" %>
… and display uploaded images in your view:
<%= image_tag url_for_file_column("entry", "image") %>
However, you may want to protect against the model object having no uploaded image:
<%= image_tag url_for_file_column("entry", "image") if @entry.image %>
To resize every uploaded image to a maximum size of 640×480, you just have to declare an additional option.
class Entry < ActiveRecord::Base
file_column :image, :magick => { :geometry => "640x480>" }
end
You can even automatically create versions in different sizes that have nice filenames…
class Entry < ActiveRecord::Base
file_column :image, :magick => { :versions => { "thumb" => "50x50", "medium" => "640x480>" } }
end
… and display them in your view:
<%= image_tag url_for_file_column("image", "entry") %>
| Print article | This entry was posted by Akhil Bansal on June 22, 2006 at 11:42 pm, and is filed under ROR, Rails, Rubyonrails, file_column. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 4 years ago
Hi, I’ve been using file_column alongside rmagick to create thumb and medium versions of the images uploaded int my site.
The other day I ran into a problem when I tried to upload an animated gif, in both the thumb and medium versions of the gif only the first frames are saved.
This is the code on the model that I use to create the versions:
file_column :image, :magick => { :versions => {“tiny” => “75×75″, “medium” => “550×400″}}
Do you happen to know how I could get around this?
about 4 years ago
Hi Bruno,
I have no idea how to resize animated gifs using file_column.
But I’ll tell you if got any solution
about 4 years ago
After using this code to resize my image -
file_column :file, :magick => { :versions => {
:thumb => {:size => “100×100>”},
:medium => {:size => “640×480>”}
}
my only problem is how to change the color/thikness of the border as well as padding when I display them in thumbnail size. Do you know of any code. thanks 4 the help.
about 4 years ago
I think you have to pass attributes as second argument through image_tag while displaying image
<%=image_tag(url_for_file_column(“issue”, ‘small_image’, ‘small’),:border=>4)%>about 3 years ago
I am having a strange problem with image uploads; on the remote server file_upload uploads only gif images. If I try to upload JPG/PNG then it gives an error “File invalid image” but if I click on submit button again then it uploads it.
However, it works well on my local machine.
Any ideas why it isn’t working?
about 3 years ago
to upload animated gifs correctly you need to change the file magic_file_column.rb to look something like this:
module FileColumn # :nodoc:
class BaseUploadedFile # :nodoc:
def transform_with_magick
if needs_resize?
begin
imglist = ::Magick::ImageList.new(absolute_path)
rescue ::Magick::ImageMagickError
@magick_errors ||= []
@magick_errors size[0].to_i || yres > size[1].to_i
imglist.each do |img|
img.change_geometry!(img_options[:size]) do |c, r, i|
i.resize!(c, r)
end
end
end
end
ensure
imglist.write dest_path
File.chmod options[:permissions], dest_path
end
end
end
# If you are using file_column to upload images, you can
# directly process the images with RMagick,
# a ruby extension
# for accessing the popular imagemagick libraries. You can find
# more information about RMagick at http://rmagick.rubyforge.org.
#
# You can control what to do by adding a :magick option
# to your options hash. All operations are performed immediately
# after a new file is assigned to the file_column attribute (i.e.,
# when a new file has been uploaded).
#
# To resize the uploaded image according to an imagemagick geometry
# string, just use the :size option:
#
# file_column :image, :magick => {:size => “800×600>â€}
#
# You can also create additional versions of your image, for example
# thumb-nails, like this:
# file_column :image, :magick => {:versions => {
# :thumb => {:size => “50×50″},
# :medium => {:size => “640×480>â€}
# }
#
# If you wish to crop your images with a size ratio before scaling
# them according to your version geometry, you can use the :crop directive.
# file_column :image, :magick => {:versions => {
# :square => {:crop => “1:1″, :size => “50×50″, :name => “thumbâ€},
# :screen => {:crop => “4:3″, :size => “640×480>â€},
# :widescreen => {:crop => “16:9″, :size => “640×360!â€},
# }
# }
#
# These versions will be stored in separate sub-directories, named like the
# symbol you used to identify the version. So in the previous example, the
# image versions will be stored in “thumbâ€, “screen†and “widescreenâ€
# directories, resp.
# A name different from the symbol can be set via the :name option.
#
# These versions can be accessed via FileColumnHelper’s +url_for_image_column+
# method like this:
#
#
#
# Note: You’ll need the
# RMagick extension being installed in order to use file_column’s
# imagemagick integration.
module MagickExtension
def self.file_column(klass, attr, options) # :nodoc:
require ‘RMagick’
options[:magick] = process_options(options[:magick],false) if options[:magick]
if options[:magick][:versions]
options[:magick][:versions].each_pair do |name, value|
options[:magick][:versions][name] = process_options(value, name.to_s)
end
end
state_method = “#{attr}_stateâ€.to_sym
after_assign_method = “#{attr}_magick_after_assignâ€.to_sym
klass.send(:define_method, after_assign_method) do
self.send(state_method).transform_with_magick
end
options[:after_upload] ||= []
options[:after_upload] options } if options.kind_of?(String)
if options[:geometry]
options[:size] = options.delete(:geometry)
end
if options[:name].nil? and create_name
if create_name == true
hash = 0
for key in [:size, :crop]
hash = hash ^ options[key].hash if options[key]
end
options[:name] = hash.abs.to_s(36)
else
options[:name] = create_name
end
end
options
end
end
end
about 3 years ago
How does one go about enforcing that only images can be uploaded with file_column and also limit the size? I have been trying to find documentation on this, but only seem to find work arounds for people trying to make thumbnails for a column when ANY file types are allowed. I want to limit the types.