Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby file_get_contents equivalent

I need to use this in my rails program so I can get the image contents and then base64 it. I know how to base64 it but I just don't know how I would get the image. Anyone know how?

like image 208
deuces Avatar asked Sep 20 '09 22:09

deuces


2 Answers

Edited to retrieve from external URL:

PHP:

$image = file_get_contents("http://www.example.com/file.png");

Ruby:

require 'net/http'
image = Net::HTTP.get_response(URI.parse("http://www.example.com/file.png")).body
like image 66
jimyi Avatar answered Oct 23 '22 20:10

jimyi


For http/https/ftp you can use OpenURI module:

require "open-uri"
image = open("http://www.example.com/file.png").read
like image 35
Petr Avatar answered Oct 23 '22 20:10

Petr