enhance 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ Enhance!
2
+ ========
3
+
4
+ > "Enhance!" - Horatio Caine, CSI
5
+
6
+ [Watch this, it's awesome](http://www.youtube.com/watch?v=Vxq9yj2pVWk)
7
+
8
+ Enhance! is a Rack middleware that resizes your images on-the-fly using ImageMagick.
9
+
10
+
11
+ Rails
12
+ -----
13
+
14
+ In your application.rb file
15
+
16
+ config.middleware.use "Enhance::Enhancer", Rails.root, :routes => [:attachments, :images]
17
+
18
+
19
+ Config Options
20
+ --------------
21
+
22
+ Enhance::Enhancer.new next_in_chain, root, options
23
+
24
+ * next_in_chain: Next Rack Application to use
25
+ * root: Where the paths are constructed from
26
+ * options: described next
27
+
28
+ Options
29
+ -------
30
+
31
+ * extensions: list of supported extensions | [jpg, png, jpeg, gif]
32
+ * routes: list of matched routes | images
33
+ * quality: quality of output images | 100
34
+ * folders: list of folders to look in | ["public"]
35
+ * command_path: path for imagemagick if not in PATH | Paperclip config if available
36
+ * cache: folder in which to cache enhanced images | "#{root}/tmp/enhanced"
37
+ * max_side: maximum size of the enhanced image | 1024
38
+ * file_root: root of the server if not the same as root | root
39
+
40
+
41
+ Future changes
42
+ --------------
43
+
44
+ * I'll probably redo the config to use a block instead of a hash.
@@ -1,17 +1,26 @@
1
-
1
+ require 'cgi'
2
2
  class Enhance::Enhancer
3
3
 
4
4
  Geometry = /^(?<geometry>(?<width>\d+)?x?(?<height>\d+)?([\>\<\@\%^!])?)(?<filter>sample)?$/
5
5
 
6
+ ## Options
7
+ # extensions : list of supported extensions
8
+ # routes : list of matched routes
9
+ # folders : list of folders to look in
10
+ # quality : quality of output images
11
+ # command_path : path for imagemagick if not in PATH
12
+ # cache : folder in which to cache enhanced images
13
+ # max_side : maximum size of the enhanced image
14
+ # file_root : root of the server if not the same as root
6
15
  def initialize app, root, options = {}
7
16
  @app = app
8
- @extensions = options[:extensions] || %w( jpg png jpeg gif )
17
+ @extensions = [options[:extensions]].flatten || %w( jpg png jpeg gif )
9
18
  @routes = options[:routes] || %w( images )
10
- @folders = options[:folders] || [File.join(root, "public")]
19
+ @folders = [options[:folders]].flatten || [File.join(root, "public")]
11
20
  @quality = options[:quality] || 100
12
21
  @command_path = options[:convert_path] || "#{Paperclip.options[:command_path] if defined?(Paperclip)}"
13
- @command_path += "/" unless @command_path.blank?
14
- @cache = options[:cache] || File.join(root, "tmp")
22
+ @command_path += "/" unless @command_path.empty?
23
+ @cache = options[:cache] || File.join(root, "tmp", "enhanced")
15
24
  @max_side = options[:max_side] || 1024
16
25
  @file_root = (options[:file_root] || root).to_s
17
26
  @server = Rack::File.new(@file_root)
@@ -32,7 +41,8 @@ class Enhance::Enhancer
32
41
  File.exists?(f) ? f : nil
33
42
  end
34
43
 
35
- if request && (filename = convert(request, matches['filename'], CGI.unescape(matches['geometry']))) && filename.gsub!(@file_root, '')
44
+
45
+ if request && (filename = convert(request, matches['filename'], CGI.unescape(matches['geometry']))) && filename.gsub!(/^#{@file_root}/, '')
36
46
  env["PATH_INFO"] = filename
37
47
  @server.call env
38
48
  else
@@ -4,4 +4,4 @@ module ActionView::Helpers::UrlHelper
4
4
  [[matches['url'], geometry].compact.join("/"), matches['get']].compact.reject(&:empty?).join("?")
5
5
  end
6
6
  alias_method :enhance!, :enhance
7
- end
7
+ end if defined? ActionView
@@ -1,3 +1,3 @@
1
1
  module Enhance
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
Binary file
File without changes
@@ -0,0 +1,70 @@
1
+ require 'rack/test'
2
+ require 'test/unit'
3
+
4
+ require File.expand_path('../../lib/enhance.rb', __FILE__)
5
+
6
+ class FourOhFourApp
7
+
8
+ def call env
9
+ [404, [], ["Not found"]]
10
+ end
11
+
12
+ end
13
+
14
+ class EnhanceTest < Test::Unit::TestCase
15
+ include Rack::Test::Methods
16
+
17
+ def app
18
+ Enhance::Enhancer.new( FourOhFourApp.new, File.dirname(__FILE__),
19
+ :cache => File.join(File.dirname(__FILE__), "tmp"),
20
+ :folders => File.dirname(__FILE__)
21
+ )
22
+ end
23
+
24
+ def test_404
25
+ get "/images/unknown.jpeg"
26
+ assert last_response.not_found?
27
+ end
28
+
29
+ def test_image_width_unconstrained
30
+ get "/images/test.jpg/200x"
31
+ assert_size 200, nil
32
+ end
33
+
34
+ def test_image_height_unconstrained
35
+ get "/images/test.jpg/x200"
36
+ assert_size nil, 200
37
+ end
38
+
39
+ def test_image_force_square
40
+ get "/images/test.jpg/200x200!"
41
+ assert_size 200, 200
42
+ end
43
+
44
+ def test_image_larger_than_nature
45
+ get "/images/test.jpg/1000"
46
+ assert_size 1000, nil
47
+ end
48
+
49
+ def test_image_larger_constrained
50
+ get "/images/test.jpg/1000%3E" #1000<
51
+ assert_size 800, nil
52
+ end
53
+
54
+ def test_image_oversize
55
+ get "/images/test.jpg/2000"
56
+ assert_size 2000, nil, :<
57
+ end
58
+
59
+ def assert_size width, height, comparator = :==
60
+ dump = File.join(File.dirname(__FILE__), "results", "dump.jpg")
61
+ File.open(dump, "wb") do |f|
62
+ f.write last_response.body
63
+ end
64
+ identify = `identify #{dump}`.match /\s(?<width>[0-9]+)x(?<height>[0-9]+)/
65
+
66
+ assert identify[:width].to_i.send(comparator, width) if width
67
+ assert identify[:height].to_i.send(comparator, height) if height
68
+ end
69
+
70
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enhance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-08-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &2169505760 !ruby/object:Gem::Requirement
16
+ requirement: &2160890120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2169505760
24
+ version_requirements: *2160890120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: utilities
27
- requirement: &2169505280 !ruby/object:Gem::Requirement
27
+ requirement: &2160889660 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2169505280
35
+ version_requirements: *2160889660
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rack-test
38
- requirement: &2169504820 !ruby/object:Gem::Requirement
38
+ requirement: &2160889180 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,19 +43,22 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2169504820
46
+ version_requirements: *2160889180
47
47
  description: Middleware to "enhance" and resize images on the fly.
48
48
  email: gmalette@gmail.com
49
49
  executables: []
50
50
  extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
- - README
53
+ - README.md
54
54
  - enhance.gemspec
55
55
  - lib/enhance.rb
56
56
  - lib/enhance/enhancer.rb
57
57
  - lib/enhance/urlhelper.rb
58
58
  - lib/enhance/version.rb
59
+ - test/images/test.jpg
60
+ - test/results/.gitkeep
61
+ - test/test.rb
59
62
  homepage: ''
60
63
  licenses: []
61
64
  post_install_message:
@@ -80,4 +83,7 @@ rubygems_version: 1.8.6
80
83
  signing_key:
81
84
  specification_version: 3
82
85
  summary: Image resizing on the fly
83
- test_files: []
86
+ test_files:
87
+ - test/images/test.jpg
88
+ - test/results/.gitkeep
89
+ - test/test.rb