I have a URL to an image which i want to save locally, so that I can use Paperclip to produce a thumbnail for my application. What's the best way to download and save the image? (I looked into ruby file handling but did not come across anything.)
Try this:
require 'open-uri'
open('image.png', 'wb') do |file|
file << open('http://example.com/image.png').read
end
IO.copy_stream(open('http://example.com/image.png'), 'destination.png')
–
Pia require 'open-uri'
, just used the code under it –
Typical An even shorter version:
require 'open-uri'
download = open('http://example.com/image.png')
IO.copy_stream(download, '~/image.png')
To keep the same filename:
IO.copy_stream(download, "~/#{download.base_uri.to_s.split('/')[-1]}")
open
. I replaced it with URI.parse(url).open
instead. Read more about that cop here: rubydoc.info/gems/rubocop/RuboCop/Cop/Security/Open –
Runoff open
does not work for me, had to use URI.open
–
Wholehearted If you're using PaperClip, downloading from a URL is now handled automatically.
Assuming you've got something like:
class MyModel < ActiveRecord::Base
has_attached_file :image, ...
end
On your model, just specify the image as a URL, something like this (written in deliberate longhand):
@my_model = MyModel.new
image_url = params[:image_url]
@my_model.image = URI.parse(image_url)
You'll probably want to put this in a method in your model. This will also work just fine on Heroku's temporary filesystem.
Paperclip will take it from there.
source: paperclip documentation
I think this is the clearest way:
require 'open-uri'
File.write 'image.png', open('http://example.com/image.png').read
Possibly the simplest way:
require 'open-uri'
image_url = "https://i.imgur.com/ZWnhY9T.png"
IO.copy_stream(URI.open(image_url), 'destination.png')
img_url
by URI.open()
, i got the following ERROR No such file or directory @ rb_sysopen
. –
Peevish Check out Net::HTTP in the standard library. The documentation provides several examples on how to download documents using HTTP.
Kernel#open
enables not only file access but also process invocation by prefixing a pipe symbol (e.g., open("| ls")
). So, it may lead to a serious security risk by using variable input to the argument of Kernel#open
. –
Campball Using Ruby 3 and above, you'll get the following error using the accepted answer:
No such file or directory @ rb_sysopen - http://example.com/image.png (Errno::ENOENT)
The solution is to use URI.open
in place of the Kernel.open
. Example:
require "uri"
download = URI.open('http://example.com/image.png')
File.write('~/image.png', download)
All above examples are great. In my case I just wanted to create a download link from the image from URL.
If you want to make it downloadable (to your downloads folder), you can use the following code in your controller:
require 'open-uri'
file_type = url.to_s.split(".")[-1]
send_data open(url).read, filename: "some_name.#{file_type}", type: "image/#{file_type}", disposition: "attachment"
© 2022 - 2024 — McMap. All rights reserved.