Ruby Forum Ruby > Copy image from a website

Posted by Steve Pugh (studlee2)
on 19.11.2007 19:00
(Received via mailing list)
I'm trying to copy an image from a website, but can't seem to get it
right.

I know rio can copy a website in one line, but what about images?
Anyone have a good snippet using open-uri or something?
Posted by Jano Svitok (Guest)
on 19.11.2007 22:21
(Received via mailing list)
On Nov 19, 2007 7:00 PM, _Stud <studlee2@gmail.com> wrote:
> I'm trying to copy an image from a website, but can't seem to get it
> right.
>
> I know rio can copy a website in one line, but what about images?
> Anyone have a good snippet using open-uri or something?

web page = (virtual) file
image = (virtual) file

just use open-uri + read and write the contents somewhere.

require 'open-uri'

File.open('logo.gif', 'wb') do |f|
  f.write(open('http://www.google.com/intl/en_ALL/images/logo.gif').read)
end

J.
Posted by Dido Sevilla (Guest)
on 21.11.2007 08:35
(Received via mailing list)
On Nov 20, 2007 2:00 AM, _Stud <studlee2@gmail.com> wrote:
> I'm trying to copy an image from a website, but can't seem to get it
> right.
>
> I know rio can copy a website in one line, but what about images?
> Anyone have a good snippet using open-uri or something?

Using mechanize:

require 'mechanize'

agent = WWW::Mechanize.new
img = agent.get("http://www.example.com/image.jpg")
img.save

will load the JPEG image image.jpg from the site www.example.com and
save it as image.jpg on the local filesystem.