Plugin: file_column

June 22, 2006 at 11:42 pm 8 comments

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 => “640×480>” }
end

You can even automatically create versions in different sizes that have nice filenames…

class Entry < ActiveRecord::Base
file_column :image, :magick => {
:versions => { “thumb” => “50×50”, “medium” => “640×480>” }
}
end

… and display them in your view:

<%= image_tag url_for_file_column(“image”, “entry”) %>

Entry filed under: rails, rails plugins, Ruby, RubyonRails. Tags: .

Rails: Issuing direct query to database How to search for a string on multiple fields across multiple joined tables in Rails

8 Comments Add your own

  • 1. Bruno  |  August 11, 2006 at 6:08 pm

    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?

    Reply
  • 2. Akhil Bansal  |  August 12, 2006 at 5:47 am

    Hi Bruno,
    I have no idea how to resize animated gifs using file_column.
    But I’ll tell you if got any solution

    Reply
  • 3. cafgu  |  August 20, 2006 at 3:08 pm

    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.

    Reply
  • 4. Akhil Bansal  |  August 21, 2006 at 5:33 am

    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)%>

    Reply
  • 5. Nas  |  September 9, 2006 at 9:58 pm

    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?

    Reply
  • 6. Trung  |  December 8, 2006 at 3:05 am

    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

    Reply
  • 7. Quincy  |  December 12, 2006 at 8:58 pm

    What I want to do on my blog, is every few hours take the oldest post and move it to the
    front of the queue, all automatically. Anyone know if there is a plugin that can do this or
    a simple way to set up another plugin to do this (use my own feed perhaps)?
    Thanks.

    Reply
  • 8. Dag Stensson  |  January 29, 2008 at 1:17 pm

    I’m quite new with ruby, but I’m learning quite fast, I’ve checked a lot of differnt sites about this problem but still its not clear.
    I’ve succeeded with using the file_column for one single picture, but I haven’t quite got how to use “has_many :entries” with file_column. I more than often get two different types of fail:

    I’m well aware that the models need to have “has_many” and “belongs_to” and these are right. but with only “has_many :entries” (and belongs_to :user) I recieve “unkown method entry”.

    So I’ve tried a couple of diffrent ways to figure out where they should be called “entries” and “entry”, but more than it results in “‘image_relative_path’ for Entry:Class”

    now what’s “image_relative_path” all about? Is this possible at all with file_column?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Important Notice…

I have moved this blog to http://webonrails.com . I will continue posting at http://webonrails.com, not on https://webonrails.wordpress.com.

Kindly update your book marks to http://webonrails.com

and http://webonrails.com/feed .

I am really very sorry for inconvenience.

Comming Soon….

Domain for Sale

TheRailsHosting.com


(Contact me)

Recent Posts

Calender

June 2006
M T W T F S S
« Apr   Jul »
 1234
567891011
12131415161718
19202122232425
2627282930  

Subscribe

  • Add to Google
  • Subscribe in NewsGator Online
  • Subscribe with Bloglines
  • WebOnRails on feedburner
  • technorati

del.icio.us

Feeds

Flickr Photos

A bellezza di a natura (C☺rsica)

JWL5932 Mountain Hare..

Black and Gold

More Photos

Visitor’s Location

Blog Stats

  • 17,978 hits

%d bloggers like this: