Ok, here’s what I’ve got so far, but now my RMagick is not working.
Rails gives this error:
unable to open image `public/images/image.jpg’: No such file or
directory
app/controllers/asset_controller.rb:108:in write' app/controllers/asset_controller.rb:108:in
make_thumbnail’
app/controllers/asset_controller.rb:78:in `create’
Well, it IS still saving the upload, so the file IS there! But I can’t
seem to find the error in my code. Everything I try leads me back to
the same error. My logic is flawed. (no surprise)
So here’s what’s in my controller: (tell me if more code is more
helpful)
def file_upload(ul_file)
File::File.open “#{RAILS_ROOT}/public/images/
#{ul_file.original_filename}”,‘wb’ do |f|
f.write(ul_file.read)
@asset.file_name = ul_file.original_filename
@image_to_alter = “#{RAILS_ROOT}/public/images/
#{ul_file.original_filename}”
# make_thumbnail(ul_file)
# make_thumbnail(image_to_alter)
# make_comp(image_to_alter)
end
end
def save_file(ul_file)
File.open(ul_file, ‘wb’)
end
def create
@asset = Asset.new(params[:asset])
# ul_file = File.open(params[“asset”][“file_upload”], “w”)
file_upload(params[“asset”][“file_upload”])
make_thumbnail(@image_to_alter)
if @asset.save
flash[:notice] = ‘Asset saved.’
redirect_to :action => ‘list’
else
flash[:notice] = ‘error’
render :action => ‘new’
end
end
def destroy
Asset.find(params[:id]).destroy
redirect_to :action => ‘list’
end
def make_thumbnail(image_to_alter)
#rmagick code here
# Begin thumbing
img = Image.read(image_to_alter)[0]
thumbnail_height = 100
thumbnail_width = 100
geometry_obj = Geometry.new(thumbnail_width, thumbnail_height,
nil, nil, ‘!’)
chg_geom_img = img.change_geometry(geometry_obj) {|cols, rows,
image| image.resize(cols, rows)}
chg_geom_img_name = ‘thumbnail’ + img.filename
# chg_geom_img_name.gsub!(’-’, ‘’)
# chg_geom_img_name.gsub!(’ ', '’)
if chg_geom_img.write(chg_geom_img_name)
@asset.thumbnail.file_name = chg_geom_img_name
@asset.thumbnail.file_size = File.size?("#{RAILS_ROOT}/public/
images/#{chg_geom_img_name}")
file_to_test = File.open("#{RAILS_ROOT}/public/images/
#{chg_geom_img_name}", ‘r’)
# Test the types, let's hope this works
case file_to_test
when File.jpg?(file_to_test)
my_file_type = 'jpg'
when File.gif?(file_to_test)
my_file_type = 'gif'
when File.png?(file_to_test)
my_file_type = 'png'
end
# Set the data
@asset.thumbnail.file_type = my_file_type
@asset.thumbnail.mime_type = 'image/' + my_file_type
end
end # make_thumbnail()