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.
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?
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.
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...
('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.