Plugin: file_column

Published on Author Akhil Bansal7 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…

[source:ruby] class Entry < ActiveRecord::Base
file_column :image
end [/source]… 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") %>

7 Responses to Plugin: file_column

  1. 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 =&gt; { :versions =&gt; {“tiny” =&gt; “75×75”, “medium” =&gt; “550×400”}}

    Do you happen to know how I could get around this?

  2. After using this code to resize my image –
    file_column :file, :magick =&gt; { :versions =&gt; {
    :thumb =&gt; {:size =&gt; “100×100&gt;”},
    :medium =&gt; {:size =&gt; “640×480&gt;”}
    }

    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.

  3. I think you have to pass attributes as second argument through image_tag while displaying image

    &lt;%=image_tag(url_for_file_column(“issue”, ‘small_image’, ‘small’),:border=&gt;4)%&gt;

  4. 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?

  5. 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 &gt; 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 =&gt; {:size =&gt; “800×600&gt;”}
    #
    # You can also create additional versions of your image, for example
    # thumb-nails, like this:
    # file_column :image, :magick =&gt; {:versions =&gt; {
    # :thumb =&gt; {:size =&gt; “50×50″},
    # :medium =&gt; {:size =&gt; “640×480&gt;”}
    # }
    #
    # 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 =&gt; {:versions =&gt; {
    # :square =&gt; {:crop =&gt; “1:1″, :size =&gt; “50×50″, :name =&gt; “thumb”},
    # :screen =&gt; {:crop =&gt; “4:3″, :size =&gt; “640×480&gt;”},
    # :widescreen =&gt; {:crop =&gt; “16:9″, :size =&gt; “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

  6. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *