Today I learned to do two things in ruby/rails:
– Downloading images from and external link to my hard drive
– Resizing those images
Image Download:
To download the images, I used net/http as follows:
require ‘net/http’
require ‘uri’
url = URI.parse(‘http://www.example.com/oldone.jpg’)
req = Net::HTTP::Proxy(proxy_addr, proxy_port).get_response(url)
open(“newone.jpg”, “wb”) do |file|
file.write(req.body)
end
Please note: I needed the proxy addr and port, if thats not required, the “Proxy(proxy_addr, proxy_port)” can be omitted.
Image Resize:
To resize the images, I needed to install ImageMagick and mini_magick.
(In windows, mini_magick installation was easy, got it by “gem install mini_magick”, but for ImageMagick, I got the portable copy “ImageMagick-6.6.5-Q16-windows.zip” from http://www.imagemagick.org/script/binary-releases.php, and then copied all the .dll and .exe files to my ruby/bin directory, whew! it worked then!)
Here is the code for resizing image:
image = MiniMagick::Image.from_file(dir + (“oldone” + extension))
image.resize(“60X60”)
image.write(dir + (“newone” + extension))