enhance 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +0 -0
- data/enhance.gemspec +23 -0
- data/lib/enhance.rb +9 -0
- data/lib/enhance/enhancer.rb +70 -0
- data/lib/enhance/version.rb +3 -0
- metadata +106 -0
data/README
ADDED
File without changes
|
data/enhance.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.push File.join(File.dirname(__FILE__), '/lib')
|
2
|
+
|
3
|
+
require 'enhance/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "enhance"
|
7
|
+
gem.authors = ['Guillaume Malette']
|
8
|
+
gem.date = %q{2011-08-26}
|
9
|
+
gem.description = %q{Middleware to "enhance" and resize images on the fly.}
|
10
|
+
gem.summary = "Image resizing on the fly"
|
11
|
+
gem.email = 'gmalette@gmail.com'
|
12
|
+
gem.homepage = ''
|
13
|
+
|
14
|
+
gem.add_runtime_dependency 'rack'
|
15
|
+
gem.add_runtime_dependency 'utilities'
|
16
|
+
gem.add_development_dependency 'rack-test'
|
17
|
+
|
18
|
+
gem.executables = []
|
19
|
+
gem.files = `git ls-files`.split("\n")
|
20
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
gem.require_paths = ['lib']
|
22
|
+
gem.version = Enhance::VERSION
|
23
|
+
end
|
data/lib/enhance.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
|
2
|
+
class Enhance::Enhancer
|
3
|
+
|
4
|
+
def initialize app, root, options = {}
|
5
|
+
@app = app
|
6
|
+
@extensions = options[:extensions] || %w( jpg png )
|
7
|
+
@routes = options[:routes] || %w( images )
|
8
|
+
@folders = options[:folders] || [File.join(root, "public")]
|
9
|
+
@quality = options[:quality] || 100
|
10
|
+
@command_path = options[:convert_path] || "#{Paperclip.options[:command_path] if defined?(Paperclip)}"
|
11
|
+
@command_path += "/" unless @command_path.blank?
|
12
|
+
@cache = options[:cache] || File.join(root, "tmp")
|
13
|
+
@max_side = options[:max_side] || 1024
|
14
|
+
@server = Rack::File.new("/")
|
15
|
+
end
|
16
|
+
|
17
|
+
def call env
|
18
|
+
matches = env['PATH_INFO'].match /(?<filename>(#{@routes.join("|")}).*(#{@extensions.join("|")}))\/(?<geometry>.*)/i
|
19
|
+
p matches
|
20
|
+
if matches && !matches['filename'].include?("..")
|
21
|
+
dup._call env, matches
|
22
|
+
else
|
23
|
+
@app.call env
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def _call env, matches
|
28
|
+
request = @folders.collect_first{|f| File.join(f, matches['filename']) if File.exists?(File.join(f, matches['filename']))}
|
29
|
+
|
30
|
+
if request && filename = convert(request, matches['filename'], CGI.unescape(matches['geometry']))
|
31
|
+
@server.call env
|
32
|
+
else
|
33
|
+
@app.call env
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Finds the image and resizes it if needs be
|
38
|
+
def convert path, filename, geometry
|
39
|
+
# Extract the width and height
|
40
|
+
if sizes = geometry.match(/^(?<width>\d+)?x?(?<height>\d+)([\>\<\@\%^!])?$/)
|
41
|
+
w, h = sizes['width'], sizes['height']
|
42
|
+
ow, oh = original_size path
|
43
|
+
|
44
|
+
# Resize if needed
|
45
|
+
if (w.nil? || w.to_i <= @max_side) && (h.nil? || h.to_i <= @max_side) && (ow != w || oh != h)
|
46
|
+
new_name = File.join(@cache, filename, geometry) + File.extname(filename)
|
47
|
+
resize path, new_name, geometry
|
48
|
+
else
|
49
|
+
path
|
50
|
+
end
|
51
|
+
else
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Creates the path and resizes the images
|
57
|
+
def resize source, destination, geometry
|
58
|
+
FileUtils.mkdir_p File.dirname(destination)
|
59
|
+
|
60
|
+
`#{@command_path}convert \"#{source}\" -resize \"#{geometry}\" -quality #{@quality} \"#{destination}\"` unless File.exists? destination
|
61
|
+
|
62
|
+
destination
|
63
|
+
end
|
64
|
+
|
65
|
+
# Finds the size of the original image
|
66
|
+
def original_size filename
|
67
|
+
`#{@command_path}identify -format '%w %h' #{filename}`.split(/\s/)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enhance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 0.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Guillaume Malette
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-08-26 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rack
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: utilities
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rack-test
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id003
|
59
|
+
description: Middleware to "enhance" and resize images on the fly.
|
60
|
+
email: gmalette@gmail.com
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files: []
|
66
|
+
|
67
|
+
files:
|
68
|
+
- README
|
69
|
+
- enhance.gemspec
|
70
|
+
- lib/enhance.rb
|
71
|
+
- lib/enhance/enhancer.rb
|
72
|
+
- lib/enhance/version.rb
|
73
|
+
has_rdoc: true
|
74
|
+
homepage: ""
|
75
|
+
licenses: []
|
76
|
+
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.3.7
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Image resizing on the fly
|
105
|
+
test_files: []
|
106
|
+
|