illusionist 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/illusionist +15 -0
- data/lib/illusionist.rb +72 -0
- metadata +79 -0
data/bin/illusionist
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rack'
|
4
|
+
require 'illusionist'
|
5
|
+
|
6
|
+
options = {
|
7
|
+
:Host => '127.0.0.1',
|
8
|
+
:Port => '8080'
|
9
|
+
}
|
10
|
+
|
11
|
+
foo = Illusionist.new
|
12
|
+
|
13
|
+
Rack::Handler::Thin.run(foo, options) do |server|
|
14
|
+
[:INT, :TERM].each { |sig| trap(sig) { server.stop } }
|
15
|
+
end
|
data/lib/illusionist.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'RMagick'
|
2
|
+
|
3
|
+
class Illusionist
|
4
|
+
include Magick
|
5
|
+
attr_accessor :response_code, :headers, :body
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@response_code = 200
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
full_path = Dir.pwd + env["REQUEST_PATH"]
|
13
|
+
if File.exist? full_path
|
14
|
+
self.response_code = 200
|
15
|
+
content_type = `file -Ib #{full_path}`.gsub(/\n/,"")
|
16
|
+
self.headers = {"Content-Type" => content_type}
|
17
|
+
|
18
|
+
#parse query string
|
19
|
+
query_hash = Hash[*env['QUERY_STRING'].split(/[&=]/)]
|
20
|
+
|
21
|
+
#check for resize commands
|
22
|
+
if(query_hash.has_key?('w') || query_hash.has_key?('width'))
|
23
|
+
#determine width and height
|
24
|
+
width = (query_hash['width'] ||= query_hash['w']).to_i
|
25
|
+
height = (query_hash['height'] ||= query_hash['h'] ||= width).to_i
|
26
|
+
#check resize mode, default to pad
|
27
|
+
mode = (query_hash['mode'] ||= 'pad')
|
28
|
+
|
29
|
+
#come up with rewrite file name
|
30
|
+
file_name = env["REQUEST_PATH"].split('.').first
|
31
|
+
extension = env["REQUEST_PATH"].split('.').last
|
32
|
+
illusion = "#{Dir.pwd}/illusions/#{file_name}_r#{width}x#{height}_#{mode}.#{extension}"
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
#create the illusion if nessicary
|
37
|
+
unless File.exists?(illusion)
|
38
|
+
#choose a resize function
|
39
|
+
resize_function = case mode
|
40
|
+
when 'max' then :resize_to_fit
|
41
|
+
when 'pad' then :pad
|
42
|
+
when 'crop' then :resize_to_fill
|
43
|
+
when 'stretch' then :scale
|
44
|
+
else :resize_to_fill
|
45
|
+
end
|
46
|
+
|
47
|
+
#call the appropriate function
|
48
|
+
if resize_function == :pad #one of these things is not like the others
|
49
|
+
source = Image.read(full_path).first().resize_to_fit!(width, height)
|
50
|
+
target = Image.new(width, height) do
|
51
|
+
self.background_color = 'white'
|
52
|
+
end
|
53
|
+
target.composite(source, CenterGravity, AtopCompositeOp).write(illusion)
|
54
|
+
else
|
55
|
+
Image.read(full_path).first().send(resize_function, width, height).write(illusion)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
#reate the file
|
60
|
+
self.body = File.new(illusion)
|
61
|
+
else
|
62
|
+
self.body = File.new(full_path)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
self.response_code = 404
|
66
|
+
self.headers = {"Content-Type" => "text/plain"}
|
67
|
+
self.body = ["#{env.inspect}"]
|
68
|
+
end
|
69
|
+
[self.response_code, self.headers, self.body]
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: illusionist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel H. Green
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rmagick
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Resizes images on the fly and caches the restults.
|
47
|
+
email: linuxdan@gmail.com
|
48
|
+
executables:
|
49
|
+
- illusionist
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- bin/illusionist
|
54
|
+
- lib/illusionist.rb
|
55
|
+
homepage: https://github.com/radfahrer/illusionist
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.24
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Rack app image server.
|
79
|
+
test_files: []
|