Upload and resize with RMagick


/ Published in: Rails
Save to your folder(s)

Use this to upload and resize your images using RMagick. Code by nixnewbie.


Copy this code and paste it in your HTML
  1. require 'RMagick'
  2. class Profile < ActiveRecord::Base
  3. belongs_to :user
  4.  
  5. #image = the image passed from params[:image]
  6. #file_type = the confirmed filetype from the controller
  7. #current_user = various information about the current user
  8. def self.upload(image, file_type, current_user)
  9. #set default cols and rows for our two images that will be created from the uploaded user image
  10. img_size = {:main =>{:cols => 250,:rows => 375},
  11. :thumb =>{:cols =>80, :rows =>120}
  12. }
  13. #set base directory for the users image
  14. imgDir = "#{RAILS_ROOT}/public/images/user_images"
  15. #set the base name for our userimage
  16. userImg = " #{current_user.login}-#{current_user.id}"
  17. #loop through the image dir and delete any old user images
  18. Dir.foreach(imgDir){|file|
  19. if file.to_s.include?(userImg)
  20. File.unlink("#{imgDir}/#{file}")
  21. end
  22. }
  23. #read the image from the string
  24. imgs = Magick::Image.from_blob(image.read)
  25. #change the geometry of the image to suit our predefined size
  26. main_image = imgs.first.change_geometry!("#{img_size[:main][:cols]}x#{img_size[:main][:rows]}") { |cols, rows, img|
  27. #if the cols or rows are smaller then our predefined sizes we build a white background and center the image in it
  28. if cols < img_size[:main][:cols] || rows < img_size[:main][:rows]
  29. #resize our image
  30. img.resize!(cols, rows)
  31. #build the white background
  32. bg = Magick::Image.new(img_size[:main][:cols],img_size[:main][:rows]){self.background_color = "white"}
  33. #center the image on our new white background
  34. bg.composite(img, Magick::CenterGravity, Magick::OverCompositeOp)
  35.  
  36. else
  37. #in the unlikely event that the new geometry cols and rows match our predefined size we will not set a white bg
  38. img.resize!(cols, rows)
  39. end
  40. }
  41. main_image.write "#{imgDir}/#{userImg}.#{file_type}"
  42.  
  43. thumb = imgs.first.change_geometry!("#{img_size[:thumb][:cols]}x#{img_size[:thumb][:rows]}") { |cols, rows, img|
  44. if cols < img_size[:thumb][:cols] || rows < img_size[:thumb][:rows]
  45. img.resize!(cols, rows)
  46. bg = Magick::Image.new(img_size[:thumb][:cols],img_size[:thumb][:rows]){self.background_color = "white"}
  47. bg.composite(img, Magick::CenterGravity, Magick::OverCompositeOp)
  48.  
  49. else
  50. img.resize!(cols, rows)
  51. end
  52. }
  53. thumb.write "#{imgDir}/#{userImg}-thumb.#{file_type}"
  54. if FileTest.exist?("#{imgDir}/#{userImg}.#{file_type}")
  55. return "The file was written and its name is #{userImg}.#{file_type}"
  56. else
  57. return false
  58. end
  59. end
  60. end

URL: http://pastie.caboo.se/95581

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.