Comments on: Plugin: file_column /2006/06/22/plugin-file_column/ Exploring RubyOnRails Sat, 22 Dec 2007 21:33:04 +0000 #/?v=2.0.5 by: blaine garrett /2006/06/22/plugin-file_column/#comment-938 Tue, 01 May 2007 06:14:48 +0000 /2006/06/22/plugin-file_column/#comment-938 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. 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.

]]>
by: Trung /2006/06/22/plugin-file_column/#comment-76 Fri, 08 Dec 2006 05:19:18 +0000 /2006/06/22/plugin-file_column/#comment-76 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 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

]]>
by: Nas /2006/06/22/plugin-file_column/#comment-7 Sat, 09 Sep 2006 21:58:45 +0000 /2006/06/22/plugin-file_column/#comment-7 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? 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?

]]>
by: Akhil Bansal /2006/06/22/plugin-file_column/#comment-6 Mon, 21 Aug 2006 05:33:35 +0000 /2006/06/22/plugin-file_column/#comment-6 I think you have to pass attributes as second argument through image_tag while displaying image <code> </code><%=image_tag(url_for_file_column("issue", 'small_image', 'small'),:border=>4)%> 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)%>

]]>
by: cafgu /2006/06/22/plugin-file_column/#comment-5 Sun, 20 Aug 2006 15:08:34 +0000 /2006/06/22/plugin-file_column/#comment-5 After using this code to resize my image - file_column :file, :magick => { :versions => { :thumb => {:size => "100x100>"}, :medium => {:size => "640x480>"} } 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. 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.

]]>
by: Akhil Bansal /2006/06/22/plugin-file_column/#comment-4 Sat, 12 Aug 2006 05:47:01 +0000 /2006/06/22/plugin-file_column/#comment-4 Hi Bruno, I have no idea how to resize animated gifs using file_column. But I'll tell you if got any solution Hi Bruno,
I have no idea how to resize animated gifs using file_column.
But I’ll tell you if got any solution

]]>
by: Bruno /2006/06/22/plugin-file_column/#comment-3 Fri, 11 Aug 2006 18:08:29 +0000 /2006/06/22/plugin-file_column/#comment-3 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" => "75x75", "medium" => "550x400"}} Do you happen to know how I could get around this? 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?

]]>