Hi,
So now that I’m not making ridiculous typos, I’ve got my homegrown
photo uploading system going. But now the question is, how do I
implement ImageMagick photo features? I’ve got ImageMagick and the
RMagick gem installed, but I’m not sure at what point in the process I
should implement the methods RMagick exposes?
To recap the basic file uploading code:
I have a model picture that has the following methods:
validates_format_of :content_type,
:with=>/^image/,
:message=>‘Uploading is limited to pictures’
def uploaded_picture=(picture_field)
self.name = base_part_of(picture_field.original_filename)
self.content_type = picture_field.content_type.chomp
self.data = picture_field.read
end
def base_part_of(file_name)
File.basename(file_name).gsub(/[^\w._-]/,’’)
end
Basic validation to make sure it is an image, an accessor? method to
instruct it to read the data being uploaded and a method to write the
filename.
Turns out when you type it correctly, the controller isn’t that tough
either:
def get
@picture =Picture.new
end
def picture
@picture=Picture.find(params[:id])
send_data(@picture.data,
:filename=>@picture.name,
:type=>@picture.content_type,
:disposition=>“inline”)
end
def save
@picture=Picture.new(params[:picture])
if @picture.save
redirect_to(:action=>'show', :id=> @picture.id)
else
render(:action=> :get)
end
end
def show
@picture=Picture.find(params[:id])
end
There are 3 views (index, show, get) but I’m omitting them.
If I’ve understood correctly, I need to create an Rmagick object so I
can get at RMagick’s methods. Judging from the docs, I think I would
type:
picture_converted= Magick::Image.read(?)
picture_converted.change_geometry!(‘320x240’) { |cols, rows, img|
img.resize!(cols, rows)
}
I’ve put a question mark as the argument for read because I’m not sure
what object I can tap into to manipulate it. Is it as simple as using
ActiveRecord to retrieve a particular image and loading that into
picture_converted?
picture_loaded=Picture.find(params[:id])
picture_converted=Magick::Image.read(picture_loaded)
I really appreciate the help…if any of you are in Palo Alto, I owe
you a beer or something!