aboutsummaryrefslogtreecommitdiff
path: root/herrschwarz.rb
blob: 1dd9cf7813a88fb96910796e29d6e556d82e1a82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'sinatra'
require 'chunky_png'
require 'rmagick'

COORDS = 225, 1300
SIGN_SIZE = 1443, 900
IMAGE_SIZE = 1920, 2218
OVERLAY = Magick::ImageList.new("herrschwarz.png")

def random_file_name(ext)
  (0...50).map { ('a'..'z').to_a[rand(26)] }.join + "." + ext
end

def herrschwarzify_image(image)
  background = Magick::Image.new(*IMAGE_SIZE)
  canvas = Magick::Draw.new
  canvas.composite *COORDS, *SIGN_SIZE, image
  canvas.composite 0, 0, 0, 0, OVERLAY
  canvas.draw background
  background
end

Dir.mkdir "uploads" unless Dir.exists? "uploads"

get "/" do
  erb :index
end

post "/upload" do
  return redirect "/" unless params['file'] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
  orig = Magick::ImageList.new(tmpfile.path)
  transformed = herrschwarzify_image(orig)
  filename = random_file_name("png")
  transformed.write("uploads/#{filename}")
  redirect "/uploads/#{filename}"
end

get "/uploads/:filename" do |filename|
  send_file "uploads/#{filename}", :type => :png
end