light_resizer 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +13 -1
  3. data/README.md +2 -12
  4. data/lib/light_resizer.rb +14 -2
  5. data/lib/light_resizer/carrierwave_resize.rb +5 -18
  6. data/lib/light_resizer/dimension_not_found.rb +6 -0
  7. data/lib/light_resizer/middleware.rb +10 -34
  8. data/lib/light_resizer/resizer.rb +72 -0
  9. data/lib/light_resizer/version.rb +2 -2
  10. data/light_resizer.gemspec +5 -8
  11. data/spec/dummy/.gitignore +17 -0
  12. data/spec/dummy/README.rdoc +28 -0
  13. data/spec/dummy/Rakefile +6 -0
  14. data/spec/dummy/app/assets/images/.keep +0 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  16. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  17. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  18. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  19. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  20. data/spec/dummy/app/mailers/.keep +0 -0
  21. data/spec/dummy/app/models/.keep +0 -0
  22. data/spec/dummy/app/models/concerns/.keep +0 -0
  23. data/spec/dummy/app/uploaders/photo_uploader.rb +5 -0
  24. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/spec/dummy/bin/bundle +3 -0
  26. data/spec/dummy/bin/rails +4 -0
  27. data/spec/dummy/bin/rake +4 -0
  28. data/spec/dummy/bin/setup +29 -0
  29. data/spec/dummy/config.ru +4 -0
  30. data/spec/dummy/config/application.rb +19 -0
  31. data/spec/dummy/config/boot.rb +3 -0
  32. data/spec/dummy/config/environment.rb +5 -0
  33. data/spec/dummy/config/environments/development.rb +38 -0
  34. data/spec/dummy/config/environments/production.rb +74 -0
  35. data/spec/dummy/config/environments/test.rb +39 -0
  36. data/spec/dummy/config/initializers/assets.rb +11 -0
  37. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  38. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  39. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  40. data/spec/dummy/config/initializers/inflections.rb +16 -0
  41. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  42. data/spec/dummy/config/initializers/session_store.rb +3 -0
  43. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  44. data/spec/dummy/config/locales/en.yml +23 -0
  45. data/spec/dummy/config/routes.rb +56 -0
  46. data/spec/dummy/config/secrets.yml +22 -0
  47. data/spec/dummy/db/seeds.rb +7 -0
  48. data/spec/dummy/lib/assets/.keep +0 -0
  49. data/spec/dummy/lib/tasks/.keep +0 -0
  50. data/spec/dummy/public/404.html +67 -0
  51. data/spec/dummy/public/422.html +67 -0
  52. data/spec/dummy/public/500.html +66 -0
  53. data/spec/dummy/public/favicon.ico +0 -0
  54. data/spec/dummy/public/robots.txt +5 -0
  55. data/spec/dummy/public/uploads/test/.gitignore +1 -0
  56. data/spec/dummy/public/uploads/test/dragon.jpg +0 -0
  57. data/spec/features/carrier_wave_resize_spec.rb +20 -0
  58. data/spec/features/middleware_spec.rb +82 -0
  59. data/spec/spec_helper.rb +16 -5
  60. metadata +116 -79
  61. data/lib/light_resizer/image_loader.rb +0 -48
  62. data/lib/light_resizer/image_loader/original_image.rb +0 -39
  63. data/lib/light_resizer/image_loader/resize_image.rb +0 -51
  64. data/lib/light_resizer/middleware/path.rb +0 -71
  65. data/lib/light_resizer/middleware/resizer.rb +0 -45
  66. data/spec/fixtures/sample.jpg +0 -0
  67. data/spec/integration/middleware_spec.rb +0 -37
  68. data/spec/unit/image_loader_original_spec.rb +0 -29
  69. data/spec/unit/image_loader_resized_spec.rb +0 -45
  70. data/spec/unit/image_loader_spec.rb +0 -23
  71. data/spec/unit/middleware_path_spec.rb +0 -53
  72. data/spec/unit/middleware_resizer_spec.rb +0 -35
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f9da4815c65b5008c9d4c74001dc936ec747345
4
- data.tar.gz: 20eee44a94471ee5e82465d1db90cab5e771a3c6
3
+ metadata.gz: b76f9ca9dac0e858dc5685b89384519631138d9a
4
+ data.tar.gz: db86d17a4b10a770456959f9903913fcefdf92f9
5
5
  SHA512:
6
- metadata.gz: b3455cf873d8f53267062c83d9f2813cd2b7bb5146a5363dafa3ba6f935b86273cd18a38b9d2f483de7f91c24b6698bdb4f5c40c045b055674546a95a78ac9f5
7
- data.tar.gz: ea80db8b2d6d8dce3d826eae8bef9e54a415ce29cf39fb3a5bd7044691e3fab0175a70165511ecb6d308b3232f8bc8e1e5471d7e9d86dd72679979e0c51a0296
6
+ metadata.gz: 3440f1e37a3de3e0fa4de3e41c487c676eccb776360960cff5e45ad3fe6e7679875ea8148132cabcd9d9261de8feda1f93520edf205c26fc6245927f5abc5c61
7
+ data.tar.gz: 05848c633240e9f965168b82403c755acfe43fd0642e05d996ce1e061da3458552c4383f2b78c213d9f98be06459b5ea886bd3b9d3d4538c75c671d3db13cf11
data/Gemfile CHANGED
@@ -1,4 +1,16 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in light_resizer.gemspec
4
- gemspec
4
+ gemspec
5
+
6
+ # Development gems
7
+ group :development, :test do
8
+ gem 'rails'
9
+ gem 'capybara'
10
+ gem 'bundler'
11
+ gem 'rake'
12
+ gem 'rspec'
13
+ gem 'coveralls'
14
+ gem 'pry'
15
+ gem 'carrierwave'
16
+ end
data/README.md CHANGED
@@ -24,21 +24,11 @@ For example we have image in public folder by next url:
24
24
 
25
25
  Url of resized image will be:
26
26
 
27
- >example.com/images/**light_resize/100x150_kitten.jpg**
27
+ >example.com/images/**light_resize/100x150/kitten.jpg**
28
28
 
29
29
  light_resizer will find */public/images/kitten.jpg* and create *light_resize* with resized to size of 100x150px image.
30
30
 
31
- If you change extension of original image:
32
-
33
- >example.com/images/light_resize/100x150_kitten.**png**
34
-
35
- light_resize will convert image to a new extension.
36
-
37
31
  Default resize doesn't crop image and fill new space with transparent background.
38
32
 
39
33
  To crop image use '*crop*' in url:
40
- >example.com/images/light_resize/100x150_crop_kitten.png
41
-
42
-
43
-
44
-
34
+ >example.com/images/light_resize/crop/100x150/kitten.png
@@ -1,8 +1,20 @@
1
1
  require 'light_resizer/version'
2
+ require 'light_resizer/resizer'
3
+ require 'light_resizer/dimension_not_found'
2
4
  require 'light_resizer/middleware'
3
5
  require 'light_resizer/carrierwave_resize'
4
- require 'RMagick'
6
+ require 'rmagick'
7
+ require 'image_optimizer'
8
+ require 'configurations'
5
9
 
6
10
  module LightResizer
7
11
 
8
- end
12
+ include Configurations
13
+
14
+ configurable Array, :allowed_dimensions
15
+
16
+ configuration_defaults do |default_config|
17
+ default_config.allowed_dimensions = []
18
+ end
19
+
20
+ end
@@ -2,32 +2,19 @@ module LightResizer
2
2
  module CarrierWaveResize
3
3
 
4
4
  def resize_without_crop(width, height)
5
- filename_prefix = "#{width}x#{height}_"
6
- resized_image_path(filename_prefix, 'png')
5
+ resized_image_path(width, height, false)
7
6
  end
8
7
 
9
8
  alias_method :resize, :resize_without_crop
10
9
 
11
10
  def resize_with_crop(width, height)
12
- filename_prefix = "#{width}x#{height}_crop_"
13
- resized_image_path(filename_prefix)
11
+ resized_image_path(width, height, true)
14
12
  end
15
13
 
16
- def crop(image_path, x, y, width, height)
17
- image = ::Magick::Image.read(image_path).first
18
- image.crop!(x.to_i, y.to_i, width.to_i, height.to_i)
19
- image.write("#{Rails.root}/tmp/cache/#{x}_#{y}_#{width}_#{height}_#{/([^\/]*)$/.match(image_path)[1]}")
20
- File.open(image.filename)
21
- end
22
-
23
- private
14
+ protected
24
15
 
25
- def resized_image_path(prefix, extension = 'png')
26
- if url
27
- filename = File.basename(url, '.*')
28
- new_filename = "#{prefix}#{filename}.#{extension}"
29
- File.join(File.dirname(url), 'light_resize', new_filename)
30
- end
16
+ def resized_image_path(width, height, with_crop)
17
+ File.join(File.dirname(url), 'light_resizer', with_crop ? 'crop' : '', "#{width}x#{height}", file.filename) if url
31
18
  end
32
19
 
33
20
  end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ module LightResizer
3
+ class DimensionNotFound < Exception
4
+
5
+ end
6
+ end
@@ -1,47 +1,23 @@
1
1
  # encoding: utf-8
2
- require 'light_resizer/image_loader'
3
- require 'light_resizer/middleware/path'
4
- require 'light_resizer/middleware/resizer'
5
-
6
2
  module LightResizer
7
3
  class Middleware
8
4
 
9
- def initialize(app, root, folder = 'public')
10
- @app = app
11
- @image_loader = LightResizer::ImageLoader.new File.join(root, folder)
12
- @path = LightResizer::Middleware::Path.new
13
- @resizer = LightResizer::Middleware::Resizer.new
5
+ def initialize(app, root)
6
+ @app = app
7
+ @public_path = app.try(:public_path) || "#{root}/public"
14
8
  end
15
9
 
16
10
  def call(env)
17
- @path.request_path = env['PATH_INFO']
18
- if @path.image_path? and resize_request?
19
- resize
20
- Rack::File.new(@image_loader.resized.root_dir).call(env) #todo check
21
- else
22
- @app.call(env)
23
- end
24
-
25
- end
26
-
27
- def resize_request?
28
- @image_loader.original_image_path = @path.image_path
29
- @image_loader.resize_prefix = @path.prefix
30
- @image_loader.original_image_exist?
11
+ resize_options = env['PATH_INFO'].match resize_url_regex
12
+ LightResizer::Resizer.new(@public_path, resize_options).process unless resize_options.nil?
13
+ @app.call(env)
31
14
  end
32
15
 
33
- private
16
+ protected
34
17
 
35
- def resize
36
- @resizer.resize(
37
- @path.dimensions,
38
- @image_loader.original_path,
39
- @image_loader.resize_path,
40
- @path.crop_path?,
41
- @path.image_extension,
42
- @path.convert_path?
43
- ) unless @image_loader.resized_image_exist?
18
+ def resize_url_regex
19
+ /^(?<directory>\/.+?)\/light_resizer\/(?<crop>crop\/)?(?<width>\d+)x(?<height>\d+)\/(?<image>[^\/]+)$/
44
20
  end
45
21
 
46
22
  end
47
- end
23
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ module LightResizer
3
+ class Resizer
4
+
5
+ def initialize(public_path, options)
6
+ @public_path = public_path
7
+ @options = options
8
+ end
9
+
10
+ def process
11
+ return if image_exists?
12
+ resize_or_crop
13
+ create_directory
14
+ save_image
15
+ end
16
+
17
+ protected
18
+
19
+ def image_exists?
20
+ File.exists? full_path
21
+ end
22
+
23
+ def resize_or_crop
24
+ check_allowed_dimension
25
+ @options[:crop].nil? ? resize : crop
26
+ end
27
+
28
+ def check_allowed_dimension
29
+ unless LightResizer.configuration.allowed_dimensions.empty? || LightResizer.configuration.allowed_dimensions.include?([width, height])
30
+ raise DimensionNotFound, "Dimension #{width}x#{height} is not allowed"
31
+ end
32
+ end
33
+
34
+ def crop
35
+ image.resize_to_fill! width, height
36
+ end
37
+
38
+ def resize
39
+ image.resize_to_fit! width, height
40
+ image.background_color = 'Transparent'
41
+ # does not work with simple gravity
42
+ @image = image.extent width, height, (image.columns - width) / 2, (image.rows - height) / 2
43
+ end
44
+
45
+ def create_directory
46
+ directory_name = File.dirname(full_path)
47
+ FileUtils.mkpath(directory_name) unless Dir.exist? directory_name
48
+ end
49
+
50
+ def save_image
51
+ image.write full_path
52
+ ImageOptimizer.new(full_path, quiet: true).optimize
53
+ end
54
+
55
+ def image
56
+ @image ||= ::Magick::Image.read("#{@public_path}#{@options[:directory]}/#{@options[:image]}").first
57
+ end
58
+
59
+ def full_path
60
+ @full_path ||= "#{@public_path}#{@options}"
61
+ end
62
+
63
+ def width
64
+ @width ||= @options[:width].to_i
65
+ end
66
+
67
+ def height
68
+ @height ||= @options[:height].to_i
69
+ end
70
+
71
+ end
72
+ end
@@ -1,3 +1,3 @@
1
1
  module LightResizer
2
- VERSION = '0.1.7'
3
- end
2
+ VERSION = '0.2.0'
3
+ end
@@ -18,12 +18,9 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_dependency 'rmagick', '~> 2.13'
22
- spec.add_dependency 'rack', '~> 1.5'
21
+ spec.add_dependency 'rmagick'
22
+ spec.add_dependency 'rack'
23
+ spec.add_dependency 'image_optimizer', '~> 1.3.0'
24
+ spec.add_dependency 'configurations', '~> 2.2.0'
23
25
 
24
- spec.add_development_dependency 'bundler', '~> 1.5'
25
- spec.add_development_dependency 'rake'
26
- spec.add_development_dependency 'rspec', '~> 3.0'
27
- spec.add_development_dependency 'coveralls'
28
- spec.add_development_dependency 'pry'
29
- end
26
+ end
@@ -0,0 +1,17 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
17
+ /log/
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ class PhotoUploader < CarrierWave::Uploader::Base
3
+ include ::LightResizer::CarrierWaveResize
4
+
5
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6
+
7
+ Dir.chdir APP_ROOT do
8
+ # This script is a starting point to setup your application.
9
+ # Add necessary setup steps to this file:
10
+
11
+ puts "== Installing dependencies =="
12
+ system "gem install bundler --conservative"
13
+ system "bundle check || bundle install"
14
+
15
+ # puts "\n== Copying sample files =="
16
+ # unless File.exist?("config/database.yml")
17
+ # system "cp config/database.yml.sample config/database.yml"
18
+ # end
19
+
20
+ puts "\n== Preparing database =="
21
+ system "bin/rake db:setup"
22
+
23
+ puts "\n== Removing old logs and tempfiles =="
24
+ system "rm -f log/*"
25
+ system "rm -rf tmp/cache"
26
+
27
+ puts "\n== Restarting application server =="
28
+ system "touch tmp/restart.txt"
29
+ end