Plugin: file_column

Published on Author Akhil Bansal19 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") %>

19 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?

    • Είδα στα κανάλια Αβραμόπουλο και Άρη ÃŽÂÃŽÂ€£ŽÂ·Ãûιωτόπουλο και ηρέμησα! Είναι πανέτοιμη για τα δύσκολα η ΝΔ…..

    • Ann, the church has agreed to provide reasonable and affordable college tuition to my children if I agree to malign people who attack the mountain meadows massacre.Margaret, you say, “[Helen] appears old enough to have children and grandchildren, all of whom will probably share the offense and carry it.” But isn’t that the point?

    • Αγαπητε ΘΑΛΑΜΟΦΥΛΑΚΑ, ωs προs το θεμα καποιων αναγνωρισιμων πρoσωπικοτητων, το οποιο εθεσαν ανωτερω οι συνελληνεs συμπατριωτεs καλοπροαιρετα, ερχονται στην σκεψη μου οι Στελιοs Παπαθεμεληs και Ιωαννηs Κοραντηs, που ισωs ενδιαφερονται για συμμετοχη – υποστηριξη τηs προσπαθειαs, αφου επιδεικνυουν για χρονια καποιεs ευαισθησιεs για τα εθνικα μαs θεματα και την εννοια τηs ΠΑΤΡΙΔΑΣ…. Ειναι απλα μια επιλπεον αποψη και αν δεν βοηθαει, παρακαλω αγνοειστε την…..

    • My country is one of the example of cutting internet to certain sites like torrent and file sharing. Still not as bad as China though, as China actually blocked blogspot, facebook, friendster and many social network. I do feel every individual should have their freedom to internet usage.Please check out my article:

  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

    • Esses contratos geralmente tem ren£Ãaçvoo anual. Concordo com uma linha de preços menores, não só para favorecer o torcedor menos abonado, mas para inibir a pirataria.

    • Thanks so much for this! I havent been this moved by a blog for a long time! You have got it, whatever that means in blogging. Well, You are definitely somebody that has something to say that people should hear. Keep up the wonderful work. Keep on inspiring the people!VA:F [1.6.5_908]please wait…VA:F [1.6.5_908](from 0 votes)

    • Remember the one where the zombie actually came out of the screen at the theater? Had to sleep in bed with my parents after that. This was a very well-made show (it aired after the equally amazing SPACE CASES!). A shame that Nickelodeon programming has taken a complete nosedive. I doubt kids these days will remember 2000′s programming because a) it’s crap and b) they’ve been too busy with their heads down texting each other.

    • that, since the Act does not bar a revocation petition as a counterclaim, even after it has been rejected by the IPAB, i would assume that it is possible to file it. After all, the High Court can still claim to be the final adjudicator of the law. Bests,Sai.

  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 *