simple RoR problem with file_column / rmagick

At least I suspect it's a simple problem. I'm quite new. I have written a class to store images, and generate thumbnails for them. This works well. However I've been having a lot of trouble getting it to keep track of the files size.

1: class Photo < ActiveRecord::Base 2: file_column :image, :magick => { 3: :versions => { 4: "thumb" => { 5: :transformation => Proc.new { |image| 6: image.change_geometry!('160x120') { |cols, rows, img> 7: img.thumbnail!(cols, rows) 8: } 9: }, 10: :attributes => { :quality => 50 } 11: } 12: } 13: } 14: end

With rmagick it's possible to access the size like so:     Magick::ImageList.new(image_address).filesize

So I was thinking of adding a method:     def getsize         Magick::ImageList.new(image).filesize #or should this be @image?     end

Or perhaps this underneath line 5:     @size = image.filesize

However both of these trigger errors. Could someone help?

Thanks!

Aaaarg wrote:

    def getsize         Magick::ImageList.new(image).filesize #or should this be @image?     end

Or perhaps this underneath line 5:     @size = image.filesize

However both of these trigger errors. Could someone help?

Try either

Magick::ImageList.new(image).cur_image.filesize

or

Magick::Image.read(image).first.filesize

Thank you very much for the suggestions but it's not quite what I'm looking for. I think what I'm having trouble with is more basic.

What I want to be able to is access the file size (and other attributes) outside, from an rhtml file. I have done this successfully by adding this line to the rhtml file:     <%= Magick::ImageList.new(photo.image).filesize %>

Although this works, it incurs a massive time penalty, somewhere around 1 second per image.

I think the best method would be to get the size and store it when the image is saved, which is why I thought of putting this beneath line 5:      @size = image.filesize

however that does nothing so far as I can tell.

I had another thought - defining this function at the start of the class:      def store_size(image)           @size = image.filesize      end

and adding this beneath line 5:      store_size(image)

however this gives the error 'undefined method `write_description' for Photo:Class'

So how do I call a function on the object rather than the class from the block starting at line 5?

Thanks again!

add a feild 'size' to your table and this will work

class some_class

before_save :store_size

def store_size   size = image.filesize end

.... end

Hi, thanks for the reply. Still not quite what I'm looking for unfortunately.

I've posted the relevant code online for anyone willing to look:     Parked at Loopia

It is what I assumed would work, and I think it would except that when I call store_image_description from where I do, it tries to call it on the class, and not the object.

Any ideas?

Already solved?

I had to patch file_column a little bit. The Problem is, that you can't access the image anywhere but on the filesystem. "image" holds only the name of the file. So 2 options:

1. Before store: load the file again into memory and get the size 2. Patch file_column to call some "after_render" method with the image, so you can do your info extraction.

You could insert this method in magic_file_column.rb in BaseUploaded

def call_after_render(img,version)      if options[:after_render]        options[:after_render].each do |sym|          @instance.send(sym,img,version)        end      end   end

now call this method from within transform_with_magick where apropriate.

in your model you should use sth like file_column :image, :after_render => [:myinfoextractor] private def myinfoextractor(img,version)    self.width=img.columns end

I think that should give you some clue what to do...

Simple solution: attachment_fu

Ar Chron wrote:

Simple solution: attachment_fu

Q: How can I tune my Mercedes?

Sounds like a winner to me!

('course, I've always been partial to Porche's... maybe cause I got to use one for HS prom... what a memory!)

As for the Rails bit, I think Ar's right on. If you've already got rmagick working, just slap in attachment_fu. It's dirt-easy and quite powerful and configurable. If you are having to monkeypatch file_column to access size data and the like, then I really, seriously recommend attachment_fu and spend the rest of the otherwise- monkeypatch-time celebrating your fine Rails app over beers or lattes.

That's my strategy, at any rate.

-Danimal