Skip to content

Instantly share code, notes, and snippets.

@cgarvis
Last active February 2, 2022 13:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cgarvis/8925326 to your computer and use it in GitHub Desktop.
Save cgarvis/8925326 to your computer and use it in GitHub Desktop.
Image Proxy in Rails
require 'base64'
require 'net/http'
class ImageProxyController < ActionController::Base
def get
url = URI.parse(Base64.decode64(params[:url]))
image = Net::HTTP.get_response(url)
send_data image.body, type: image.content_type, disposition: 'inline'
end
end
require 'base64'
module ImageProxyHelper
def proxy_image_tag(source, options = nil
options[:src] = "/image_proxy/#{Base64.encode64(source)}"
tag("img", options)
end
end
require 'net/http'
class ProfileController < ActionController::Base
def image
if params['id'] == 'john'
url = URI.parse('https://pbs.twimg.com/profile_images/1979976243/potato_300.jpg')
elsif params['id'] == 'cgarvis'
url = URI.parse('https://pbs.twimg.com/profile_images/1809634620/twitter-profile.png')
end
unless url == nil
image = Net::HTTP.get_response(url)
send_data image.body, type: image.content_type, disposition: 'inline'
else
send_file 'default_image_path.jpg'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment