Brian Adkins
3/14/2007 5:49:00 AM
Brian Adkins wrote:
> Lucas wrote:
>> I'm developing an application that requires me to save pictures to a
>> database with other data. I've been searching for hours and tried a
>> few examples, but I keep running into trouble. Can someone recommend a
>> working example or explain what needs to happen to make this work.
>> Thank you.
>>
>
> Here are some pieces that may help to get you started.
>
> In a controller method:
>
> ...
> image = params[:listing_image]
> File.open("#{temp_file_path}", "w") do |f|
> f.write(image.read)
> end
> # Invoke Imagemagick to resize (could use RMagick if desired)
> `convert -resize 480x360 #{temp_file_path} #{file_path}`
> `convert -resize 180x135 #{file_path} #{thumb_path}`
> File.delete(temp_file_path)
> ...
>
> In a view (a key is using :multipart => true in the form)
>
> ...
> <% form_tag({ :controller => 'image', :action => 'manage_images', :id =>
> @container, :container => "#{@container.class.name}"}, { :id =>
> 'editAdForm', :name => 'editAdForm', :multipart => true }) do -%>
> ...
> <%= file_field_tag('listing_image') %>
> ...
This method stores the image in a file and a URL representing the image
in the database. If you want to store the actual image bits in the db,
you'll need to probably store the output of image.read in a blob column
- may or may not be a good idea depending on your requirements...