Topic: Image Uploads

Hello all,

  I am currently at a stage in my app where I would like to implement image uploads for users to use.  Unfortunately, I have tried attachment_fu w/ ImageScience and FreeImage and referring to Mike Clark's blog (http://clarkware.com/cgi/blosxom/2007/0 … leUploadFu) but unfortunately I keep getting errors with this combination.

  Anyone have any good recommendations as an approach to simple file/image uploads and some very minor image processing (resizing, scaling, cropping) that won't suck up too much system resources?

Thanks in advance,
Adam

Last edited by xilo32 (2007-07-10 12:13:13)

Re: Image Uploads

Any suggestions at all?

Re: Image Uploads

Hi,

I've recently written a small image-holding model for my small page, but it's very ugly. It comes from my first rails application, and frankly, the only that can be called 'working'. I use ImageMagick for postprocessing.

Here it is:

require 'RMagick'

class Photo < ActiveRecord::Base
    belongs_to :user
    acts_as_taggable
    validates_presence_of :title, :message => "Don't let the title be empty!"
    validates_uniqueness_of :permalink, :message => "Permalink should be unique"
   
    include Common # in Common there are permalink generation methods, validate method, that assigns errors to title when permalink is not unique (the user does not input the permalink themselves), and a few helpers.

    def validate_on_create
        if @file_data.blank?
            errors.add :file_data, 'Specify the file!'
        elsif not (@file_data.original_filename.downcase.match /.+\.(jpg|gif|png)/)
            errors.add :file_data, 'Extension not handled'
        end
    end


    DIRECTORY = 'public/images/gallery'
    THUMB_MAX_SIZE = [125,125]
   
    after_save :process
    after_destroy :clean_up
   
    def file_data=(file_data)
        if file_data
            @file_data = file_data
            write_attribute 'extension', file_data.original_filename.split('.').last.downcase
        else
            @file_data = nil
        end
    end
   
    def url
        path.sub(/^public/,'')
    end
   
    def thumbnail_url
        thumbnail_path.sub(/^public/,'')
    end
   
    def path
        File.join(DIRECTORY, "#{self.permalink}-full.#{extension}")
    end
   
    def thumbnail_path
        File.join(DIRECTORY, "#{self.permalink}-thumb.#{extension}")
    end
   
    private
   
    def process
        if @file_data
            create_directory
            clean_up
            save_fullsize
            create_thumbnail
            @file_data = nil
        end
    end
   
    def save_fullsize
        File.open(path,'wb') do |file|
            file.puts @file_data.read
        end
    end
   
    def create_thumbnail
        img = Magick::Image.read(path).first
        img.change_geometry!('150x150+0+0>') do |cols, rows, i|
            i.resize!(cols, rows)
        end
        img.write thumbnail_path       
    end
   
    def create_directory
        FileUtils.mkdir_p DIRECTORY
    end
   
    def clean_up
        Dir[File.join(DIRECTORY, "#{self.id}-*")].each do |filename|
            File.unlink(filename) rescue nil
        end
    end
   
end


The Photo model looks like this in the database:
class CreatePhotos < ActiveRecord::Migration
  def self.up
    create_table :photos do |t|
                        t.column "title", :string
                        t.column "permalink", :string
                        t.column "description", :text
                        t.column "filename", :string
    end
  end
  ...

I hope that helped, but remember there is a way better way to do this.
Anyway, shouldn't it be in 'Rails Programming'?

--
Micha&#322;

Last edited by komar (2007-07-10 17:45:42)

Re: Image Uploads

I think it might be.

Thanks for giving me some insight.

The main problem I've been having is installing and using an image processor along with attachment_fu.  So have you had any difficulties using ImageMagick?

Re: Image Uploads

xilo32 wrote:

So have you had any difficulties using ImageMagick?

Well, yes and no. It depends.
As for my personal (aka development) computer - no. I use gentoo linux, and installing imagemagick is just emerge imagemagick rmagick. But on production server I had fedora code 1 and that was a bit tricky:D. I compiled imagemagick libraries, and probably had to compile a few other dependency libraries as far as I remember, and then ruby magick bindings are provided through gems, so after you have ImagemMagick libraries installed, it's only a matter of gem install rmagick.

I think that ImageMagick installation pretty much depends on the OS, but it shouldn't be too complex.
http://www.imagemagick.org/script/binary-releases.php
That's a good reference, I think.

Re: Image Uploads

Hey Komar,

  Thanks for your input.  I will definitely have my hands full just trying to get the dependencies and right combination of libraries to work.

Thanks again!