scale_down 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,10 @@
1
+ == 2011-05-08 ==
2
+ Removed MiniMagick and using RMagick
3
+ MiniMagick was not returning the dimensions on some images. I was able to get RMagick to work without leaking memory
4
+
5
+ == 2011-04-03 ==
6
+ Fixed CMYK conversion of gray scale images
7
+
1
8
  == 2010-11-19 ==
2
9
 
3
10
  Changed to url schema to be
data/Gemfile.lock CHANGED
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scale_down (0.1.0)
5
- mini_magick (>= 2.3)
4
+ scale_down (0.2.1)
6
5
  rmagick (>= 2.1)
7
6
  ruby-hmac (>= 0.4.0)
8
7
  sinatra (>= 1.0)
@@ -10,30 +9,25 @@ PATH
10
9
  GEM
11
10
  remote: http://rubygems.org/
12
11
  specs:
13
- contest (0.1.2)
14
- mini_magick (2.3)
15
- subexec (~> 0.0.4)
12
+ contest (0.1.3)
16
13
  mocha (0.9.8)
17
14
  rake
18
- rack (1.2.1)
15
+ rack (1.2.2)
19
16
  rack-test (0.5.6)
20
17
  rack (>= 1.0)
21
18
  rake (0.8.7)
22
19
  rmagick (2.13.1)
23
20
  ruby-hmac (0.4.0)
24
- sinatra (1.0)
25
- rack (>= 1.0)
26
- subexec (0.0.4)
21
+ sinatra (1.2.6)
22
+ rack (~> 1.1)
23
+ tilt (>= 1.2.2, < 2.0)
24
+ tilt (1.3)
27
25
 
28
26
  PLATFORMS
29
27
  ruby
30
28
 
31
29
  DEPENDENCIES
32
30
  contest (>= 0.1.2)
33
- mini_magick (>= 2.3)
34
31
  mocha (= 0.9.8)
35
32
  rack-test (= 0.5.6)
36
- rmagick (>= 2.1)
37
- ruby-hmac (>= 0.4.0)
38
33
  scale_down!
39
- sinatra (>= 1.0)
data/README.rdoc CHANGED
@@ -1,26 +1,26 @@
1
1
  = ScaleDown
2
2
 
3
- A RESTful server for quickly scaling and serving images. Nothing more.
3
+ A Sinatra web server for scaling and serving images on demand, nothing more.
4
4
 
5
- Supports cropping images and converts CMYK to RGB.
5
+ Images are scaled based upon their URL. An HMAC signature is used to prevent unauthorized scaling of images.
6
6
 
7
- Images are scaled based upon their URL. An HMAC signature is used to prevent malicious scaling of images (ie: bring your server down.)
7
+ Supports cropping images and converts CMYK to RGB.
8
8
 
9
- The schema is simple
9
+ The URL schema
10
10
 
11
- http://:server/:geometry/:path_to_file/:filename?:hmac_signature
11
+ http://:host/:geometry/:path_to_file/:filename?:hmac_signature
12
12
 
13
- :server is the address running ScaleDown
13
+ :host is the address running ScaleDown
14
14
 
15
- :geometry is widtxheight. There can be an optional `-crop` flag attached.
15
+ :geometry is width x height and an optional `-crop`
16
16
 
17
- :path_to_file is the public path to the file
17
+ :path_to_file is the public path of the original file
18
18
 
19
19
  :filename is the name of the image to scale
20
20
 
21
- :hmac_signature is security measure to validate the request
21
+ :hmac_signature a security measure to validate the request
22
22
 
23
- For example
23
+ Example
24
24
 
25
25
  http://images.example.com/400x300/images/logo.png?A3SDACEDF
26
26
 
@@ -28,13 +28,13 @@ For example
28
28
 
29
29
  http://images.example.com/images/scaled/400x300/logo.png
30
30
 
31
- To crop an image include the 'cropped' option
31
+ To crop an image include the 'crop' option. The image will be scaled and cropped to fit the geometry.
32
32
 
33
- http://images.example.com/400x300-cropped/images/logo.png?A3SDACEDF
33
+ http://images.example.com/400x300-crop/images/logo.png?A3SDACEDF
34
34
 
35
- Use 'auto' to have an image scale to one dimension or another.
35
+ Using 'auto' as a dimension will fit the image to other dimension.
36
36
 
37
- For example, to ensure an image is 300 pixels wide
37
+ For example, to ensure an image is 300 pixels wide set the height to auto
38
38
 
39
39
  http://images.example.com/300xauto/images/logo.png/?A3SDACEDF
40
40
 
@@ -46,62 +46,51 @@ There is a very simple `/info` function for getting the image dimesions. It just
46
46
 
47
47
  gem install scale_down
48
48
 
49
- Create a rackup file (config.ru)
49
+ Create a rackup file (config.ru). See http://rubydoc.info/gems/scale_down/0.2.1/ScaleDown more options
50
50
 
51
+ require 'rubygems'
51
52
  require 'scale_down'
52
53
 
53
54
  ScaleDown.tap do |config|
54
-
55
- # This is the shared secret. Use something strong
56
- # Perhaps a visit to http://www.random.org/strings/?num=10&len=20&digits=on&upperalpha=on&format=html&rnd=new
55
+ # Shared secret for generating the HMAC signature
57
56
  config.hmac_key = "secret"
58
57
 
59
- # You can use a different HMAC, see the ruby-hmac gen
60
- config.hmac_method = HMAC::SHA1
61
-
62
- # The length of the HMAC signature to use
63
- config.hmac_length = 8
64
-
65
- # The path to the public directory
58
+ # Path to the public directory
66
59
  config.public_path = File.expand_path(File.dirname(__FILE__))+"/public"
67
-
68
- # Optional logger
69
- config.logger = Logger.new("/tmp/logfile.log")
70
60
  end
71
61
 
72
62
  run ScaleDown::Controller
73
63
 
74
- Start the server using whatever server you want.
75
-
76
- thin start -p 3002
77
-
64
+ Configure Nginx, Apache, etc to run the server. I use Passenger http://www.modrails.com/
78
65
 
79
- == Generating the URL
66
+ === Generating the URL
80
67
 
81
- In your application you will need to generate the URL for the image source.
82
-
83
- Filenames can have characters (# ? /) which may need to be escaped.
84
-
85
- Filenames should be CGI escaped before the HMAC is generated, and only escape the filename, not the path or options.
68
+ Your application will need to generate the image's URL and HMAC signature.
86
69
 
87
70
  # Example ruby function to generate signed URL
88
71
  require 'HMAC::SHA1'
89
72
  require 'cgi'
90
73
 
91
74
  # ie signed_image_url('images', 'picture.png', '400x300-cropped')
92
- def signed_image_url(path, filename, options)
93
- hmac = HMAC::SHA1.new("secret").update("/#{options}/#{path}/#{filename}").to_s[0..8]
94
- "http://images.myserver.com/#{options}/#{path}/#{CGI.escape(filename)}?#{hmac}"
75
+ def signed_image_url(path, filename, geometry)
76
+ hmac = HMAC::SHA1.new("secret").update("/#{geometry}/#{path}/#{filename}").to_s[0..8]
77
+ "http://images.myserver.com/#{geometry}/#{path}/#{CGI.escape(filename)}?#{hmac}"
95
78
  end
96
79
 
97
- Of course this could be done in PHP, Python, or whatever language your application is running.
80
+ Of course this could be done in PHP, Node, Python, or other languages.
98
81
 
99
- == TODO
82
+ == Known Issues
100
83
 
101
- Support DELETE requests to remove scaled images.
84
+ Pluses in filenames will not route properly. Filenames need to have these removed or replaced.
85
+
86
+ == TODO
102
87
 
103
88
  Custom Read/Write methods to allow for more than just local storage.
104
89
 
90
+ Examples using Node.js
91
+
92
+ There is an issue on some systems with pluses in filenames.
93
+
105
94
  == Dependencies
106
95
 
107
96
  * Sinatra
@@ -116,7 +105,7 @@ LICENSE:
116
105
 
117
106
  (The MIT License)
118
107
 
119
- Copyright © 2010 John Weir & Fame Driver LLC
108
+ Copyright © 2011 John Weir & Fame Driver LLC
120
109
 
121
110
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
122
111
 
@@ -4,35 +4,38 @@ module ScaleDown
4
4
  # Defines the method to use for generating an HMAC
5
5
  # For example
6
6
  # ScaleDown.hmac_method = HMAC::SHA2
7
+ # Default HMAC::SHA1
7
8
  attr_accessor :hmac_method
9
+ ScaleDown.hmac_method = HMAC::SHA1
8
10
 
9
11
  # The shared secret for generating the hmac
10
12
  attr_accessor :hmac_key
11
13
 
12
14
  # How many characters of the HMAC are used for validation
15
+ # Default 8
13
16
  attr_accessor :hmac_length
17
+ ScaleDown.hmac_length = 8
14
18
 
15
19
  # An array of the max width and height an image can be scaled, in pixels.
16
20
  # [800,600] would limit scaling operations to 800px wide by 600px tall
17
21
  # Default [1200,1200]
18
22
  attr_accessor :max_dimensions
23
+ ScaleDown.max_dimensions = [1200,1200]
19
24
 
20
25
  # The max file size allowed for the file to be scaled, in bytes
21
- # Defaults to 10 * 1_048_576
26
+ # Defaults 10 megabytes
22
27
  attr_accessor :max_file_size
28
+ ScaleDown.max_file_size = 10 * 1_048_576
23
29
 
24
30
  # The location of the public path for you application
25
31
  # +Must be set+
26
32
  attr_accessor :public_path
33
+
27
34
  def public_path=(str)
28
35
  @public_path = str
29
36
  ScaleDown::Controller.public = str
30
37
  end
31
38
 
32
- # Defaults
33
- ScaleDown.max_file_size = 10 * 1_048_576
34
- ScaleDown.max_dimensions = [1200,1200]
35
-
36
39
  def logger=(logger)
37
40
  @logger = logger
38
41
  end
@@ -22,8 +22,9 @@ class ScaleDown::Dispatcher
22
22
  def info(relative_path)
23
23
  path = [ScaleDown.public_path, relative_path].join("/")
24
24
  if File.exists?(path)
25
- image = MiniMagick::Image.open(path)
26
- [image[:width],image[:height]].join('x')
25
+ GC.start
26
+ image = Magick::Image.read(path).first
27
+ [image.columns, image.rows].join('x')
27
28
  else
28
29
  nil
29
30
  end
@@ -1,3 +1,3 @@
1
1
  module ScaleDown
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/scale_down.rb CHANGED
@@ -5,7 +5,6 @@ require 'rubygems'
5
5
  require 'sinatra'
6
6
  require 'logger'
7
7
  require 'RMagick'
8
- require 'mini_magick'
9
8
  require 'hmac-sha1'
10
9
 
11
10
  module ScaleDown
data/scale_down.gemspec CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency "rmagick", ">= 2.1"
18
18
  s.add_dependency "sinatra", ">= 1.0"
19
19
  s.add_dependency "ruby-hmac", ">= 0.4.0"
20
- s.add_dependency "mini_magick", ">= 2.3"
21
20
 
22
21
  s.add_development_dependency "contest", ">= 0.1.2"
23
22
  s.add_development_dependency "mocha", "0.9.8"
Binary file
@@ -12,11 +12,11 @@ class ScaleDown::Controller::Test < Test::Unit::TestCase
12
12
  ScaleDown::Dispatcher.expects(:process).with(
13
13
  :path => "user/path",
14
14
  :filename => "filename.png",
15
- :geometry => "400x300-cropped-grayscale",
15
+ :geometry => "400x300-crop-grayscale",
16
16
  :hmac => "HMAC").
17
17
  returns ["path","status"]
18
18
 
19
- get '/400x300-cropped-grayscale/user/path/filename.png?HMAC'
19
+ get '/400x300-crop-grayscale/user/path/filename.png?HMAC'
20
20
  end
21
21
  end
22
22
 
@@ -26,7 +26,7 @@ class ScaleDown::Controller::Test < Test::Unit::TestCase
26
26
  get "/geo/path/filename?hmac"
27
27
 
28
28
  assert_equal 301, last_response.status
29
- assert_equal "/image-path", last_response["Location"]
29
+ assert_equal "http://example.org/image-path", last_response["Location"]
30
30
  end
31
31
  end
32
32
 
@@ -135,18 +135,26 @@ class ScaleDown::Image::Test < Test::Unit::TestCase
135
135
  end
136
136
  end
137
137
 
138
- context "color correction" do
139
- setup do
140
- @subject = create \
138
+ context "CMYK images" do
139
+ should "be converted to RGB" do
140
+ create \
141
141
  tests_path("files/cmyk.tif"),
142
142
  tests_path("scaled_test/graphic_scaled.jpg"),
143
143
  { :width => "auto", :height => 200}
144
- end
145
144
 
146
- should "be automatic" do
147
145
  image = Magick::Image.read(tests_path("scaled_test/graphic_scaled.jpg")).first
148
146
  assert_equal Magick::RGBColorspace, image.colorspace
149
147
  end
148
+
149
+ should "convert JPGs to RGB JPEGS" do
150
+ create \
151
+ tests_path("files/cmyk_gray.jpg"),
152
+ tests_path("scaled_test/graphic_scaled_2.jpg"),
153
+ { :width => "auto", :height => 200}
154
+
155
+ image = Magick::Image.read(tests_path("scaled_test/graphic_scaled_2.jpg")).first
156
+ assert_equal Magick::RGBColorspace, image.colorspace
157
+ end
150
158
  end
151
159
 
152
160
  end
@@ -52,10 +52,16 @@ class ScaleDown::Test < Test::Unit::TestCase
52
52
  FileUtils.rm_r("/tmp/scale_down")
53
53
  end
54
54
 
55
+ should "get image info" do
56
+ FileUtils.cp tests_path("files/cmyk.tif"), "/tmp/scale_down/test_images/example_1/long name .tiff"
57
+ get "/test_images/example_1/#{CGI.escape 'long name .tiff'}/info"
58
+ assert_equal "300x500", last_response.body
59
+ end
60
+
55
61
  should "get an image and scale it" do
56
62
  valid_get '/400x300-cropped/test_images/example_1/graphic.png'
57
63
  assert_equal 301, last_response.status
58
- assert_equal "/test_images/example_1/scaled/graphic-400x300-cropped.png", last_response["Location"]
64
+ assert_match "/test_images/example_1/scaled/graphic-400x300-cropped.png", last_response["Location"]
59
65
  assert File.exists?("/tmp/scale_down/test_images/example_1/scaled/graphic-400x300-cropped.png")
60
66
  end
61
67
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scale_down
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
5
- prerelease: false
4
+ hash: 19
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Weir
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-11 00:00:00 -05:00
18
+ date: 2011-05-10 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -64,25 +64,10 @@ dependencies:
64
64
  version: 0.4.0
65
65
  type: :runtime
66
66
  version_requirements: *id003
67
- - !ruby/object:Gem::Dependency
68
- name: mini_magick
69
- prerelease: false
70
- requirement: &id004 !ruby/object:Gem::Requirement
71
- none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- hash: 5
76
- segments:
77
- - 2
78
- - 3
79
- version: "2.3"
80
- type: :runtime
81
- version_requirements: *id004
82
67
  - !ruby/object:Gem::Dependency
83
68
  name: contest
84
69
  prerelease: false
85
- requirement: &id005 !ruby/object:Gem::Requirement
70
+ requirement: &id004 !ruby/object:Gem::Requirement
86
71
  none: false
87
72
  requirements:
88
73
  - - ">="
@@ -94,11 +79,11 @@ dependencies:
94
79
  - 2
95
80
  version: 0.1.2
96
81
  type: :development
97
- version_requirements: *id005
82
+ version_requirements: *id004
98
83
  - !ruby/object:Gem::Dependency
99
84
  name: mocha
100
85
  prerelease: false
101
- requirement: &id006 !ruby/object:Gem::Requirement
86
+ requirement: &id005 !ruby/object:Gem::Requirement
102
87
  none: false
103
88
  requirements:
104
89
  - - "="
@@ -110,11 +95,11 @@ dependencies:
110
95
  - 8
111
96
  version: 0.9.8
112
97
  type: :development
113
- version_requirements: *id006
98
+ version_requirements: *id005
114
99
  - !ruby/object:Gem::Dependency
115
100
  name: rack-test
116
101
  prerelease: false
117
- requirement: &id007 !ruby/object:Gem::Requirement
102
+ requirement: &id006 !ruby/object:Gem::Requirement
118
103
  none: false
119
104
  requirements:
120
105
  - - "="
@@ -126,7 +111,7 @@ dependencies:
126
111
  - 6
127
112
  version: 0.5.6
128
113
  type: :development
129
- version_requirements: *id007
114
+ version_requirements: *id006
130
115
  description: ""
131
116
  email:
132
117
  - john@famedriver.com
@@ -154,6 +139,7 @@ files:
154
139
  - lib/scale_down/version.rb
155
140
  - scale_down.gemspec
156
141
  - test/files/cmyk.tif
142
+ - test/files/cmyk_gray.jpg
157
143
  - test/files/graphic.png
158
144
  - test/files/invalid_jpeg.jpg
159
145
  - test/files/orient.jpg
@@ -194,12 +180,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
180
  requirements: []
195
181
 
196
182
  rubyforge_project: scale_down
197
- rubygems_version: 1.3.7
183
+ rubygems_version: 1.5.2
198
184
  signing_key:
199
185
  specification_version: 3
200
186
  summary: A Sinatra based server for quickly scaling and serving images. Nothing more.
201
187
  test_files:
202
188
  - test/files/cmyk.tif
189
+ - test/files/cmyk_gray.jpg
203
190
  - test/files/graphic.png
204
191
  - test/files/invalid_jpeg.jpg
205
192
  - test/files/orient.jpg