Created
December 6, 2012 10:23
-
-
Save bugcloud/4223507 to your computer and use it in GitHub Desktop.
glitch image by giving a URL
This file contains hidden or 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 'rubygems' | |
| require 'base64' | |
| require 'json' | |
| require 'sinatra' | |
| require 'erb' | |
| require 'open-uri' | |
| require 'filemagic/ext' | |
| class App < Sinatra::Base | |
| set :haml, {:format => :html5} | |
| set :public_folder, "public" | |
| set :views, "app/views" | |
| get '/' do | |
| mime_type = "text/plain" | |
| img = nil | |
| unless params[:image_url].nil? | |
| img = open(params[:image_url]) | |
| mime_type = img.mime_type | |
| img = img.read | |
| img = glitch(img) | |
| end | |
| content_type mime_type | |
| img | |
| end | |
| post '/base64' do | |
| img = nil | |
| unless params[:base64_image].nil? | |
| img = Base64.strict_decode64(params[:base64_image]) | |
| img = glitch(img) | |
| end | |
| content_type "application/json" | |
| # support JPEG only | |
| { mime_type: "image/jpeg", base64_image: Base64.strict_encode64(img) }.to_json | |
| end | |
| helpers do | |
| def partial(page, options={}) | |
| haml page, options.merge!(:layout => false) | |
| end | |
| end | |
| private | |
| def glitch(img) | |
| replaces = params[:replacement] || '1' | |
| replaces = replaces.split ',' | |
| repeat = params[:glitch_times].to_i || 10 | |
| repeat = 1 if repeat < 0 or repeat > 300 | |
| repeat.to_i.times do | |
| img.sub! %r(#{replaces.join("|")}), rand(10).to_s | |
| end | |
| img | |
| end | |
| end | |
| App.run! if __FILE__ == $0 |
This file contains hidden or 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
| source 'http://rubygems.org' | |
| gem 'sinatra' | |
| gem 'haml' | |
| gem 'therubyracer' | |
| gem 'coffee-script' | |
| gem 'sprockets', '>= 2.0' | |
| gem 'thin' | |
| gem 'ruby-filemagic' | |
| gem 'unicorn' | |
| group :development, :test do | |
| # Pretty printed test output | |
| gem 'turn', '>= 0.8.2', :require => false | |
| gem 'rspec', '>= 2.0.0.beta.22' | |
| gem 'rack-test' | |
| gem 'cucumber' | |
| gem 'cucumber-sinatra' | |
| gem 'capybara' | |
| gem 'database_cleaner' | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment