Skip to content

Instantly share code, notes, and snippets.

@jeffa
Last active December 17, 2015 16:29
Show Gist options
  • Save jeffa/5638882 to your computer and use it in GitHub Desktop.
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.
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/&lt;image_url&gt;?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