Last active
December 17, 2015 16:29
-
-
Save jeffa/5638882 to your computer and use it in GitHub Desktop.
Create an image manipulation web service. The service should be able to proxy images from a source, manipulate them, and return a new image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'open-uri' | |
require 'RMagick' | |
include Magick | |
get '/' do | |
'This service is currently REST only. | |
You will have to manually request service by typing and entering your own URI like so: | |
<p><tt>acme.com/<image_url>?w=XX&h=XX</tt></p>' | |
end | |
get %r{([^?]+)} do | |
url = 'http:/' + params[:captures].first | |
uri = URI.parse( url ) | |
# extract extension | |
re = /\.(\w{3})$/ | |
ext = re.match( url )[1] | |
if (ext == 'jpg'); ext = 'jpeg'; end | |
str = '' | |
uri.open {|f| str = f.read } | |
img = Image.from_blob( str ).first | |
img.resize_to_fill!( params[:w].to_i, params[:h].to_i ) | |
content_type "image/#{ext}" | |
img.to_blob | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment