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.
- checksums.yaml +4 -4
- data/Gemfile +13 -1
- data/README.md +2 -12
- data/lib/light_resizer.rb +14 -2
- data/lib/light_resizer/carrierwave_resize.rb +5 -18
- data/lib/light_resizer/dimension_not_found.rb +6 -0
- data/lib/light_resizer/middleware.rb +10 -34
- data/lib/light_resizer/resizer.rb +72 -0
- data/lib/light_resizer/version.rb +2 -2
- data/light_resizer.gemspec +5 -8
- data/spec/dummy/.gitignore +17 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +16 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/uploaders/photo_uploader.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +19 -0
- data/spec/dummy/config/boot.rb +3 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +38 -0
- data/spec/dummy/config/environments/production.rb +74 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/seeds.rb +7 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/lib/tasks/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/robots.txt +5 -0
- data/spec/dummy/public/uploads/test/.gitignore +1 -0
- data/spec/dummy/public/uploads/test/dragon.jpg +0 -0
- data/spec/features/carrier_wave_resize_spec.rb +20 -0
- data/spec/features/middleware_spec.rb +82 -0
- data/spec/spec_helper.rb +16 -5
- metadata +116 -79
- data/lib/light_resizer/image_loader.rb +0 -48
- data/lib/light_resizer/image_loader/original_image.rb +0 -39
- data/lib/light_resizer/image_loader/resize_image.rb +0 -51
- data/lib/light_resizer/middleware/path.rb +0 -71
- data/lib/light_resizer/middleware/resizer.rb +0 -45
- data/spec/fixtures/sample.jpg +0 -0
- data/spec/integration/middleware_spec.rb +0 -37
- data/spec/unit/image_loader_original_spec.rb +0 -29
- data/spec/unit/image_loader_resized_spec.rb +0 -45
- data/spec/unit/image_loader_spec.rb +0 -23
- data/spec/unit/middleware_path_spec.rb +0 -53
- data/spec/unit/middleware_resizer_spec.rb +0 -35
@@ -1,48 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'light_resizer/image_loader/original_image'
|
3
|
-
require 'light_resizer/image_loader/resize_image'
|
4
|
-
|
5
|
-
module LightResizer
|
6
|
-
class ImageLoader
|
7
|
-
|
8
|
-
attr_reader :original, :resized
|
9
|
-
|
10
|
-
def initialize(root_dir)
|
11
|
-
@original = ImageLoader::OriginalImage.new(root_dir)
|
12
|
-
@resized = ImageLoader::ResizeImage.new(root_dir)
|
13
|
-
end
|
14
|
-
|
15
|
-
def original_image_path=(path)
|
16
|
-
#todo refresh @original image
|
17
|
-
@original.relative_path = path
|
18
|
-
end
|
19
|
-
|
20
|
-
def resize_prefix=(prefix)
|
21
|
-
#todo clear resize prefix
|
22
|
-
@resized.original_filename = @original.filename
|
23
|
-
@resized.original_relative_dir = @original.relative_dir
|
24
|
-
@resized.resize_prefix = prefix
|
25
|
-
end
|
26
|
-
|
27
|
-
def original_path
|
28
|
-
@original.full_path
|
29
|
-
end
|
30
|
-
|
31
|
-
def original_image_exist?
|
32
|
-
@original.image_exist?
|
33
|
-
end
|
34
|
-
|
35
|
-
def resize_path
|
36
|
-
@resized.full_path
|
37
|
-
end
|
38
|
-
|
39
|
-
def resized_image_relative_path
|
40
|
-
@resized.relative_path
|
41
|
-
end
|
42
|
-
|
43
|
-
def resized_image_exist?
|
44
|
-
@resized.image_exist?
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module LightResizer
|
3
|
-
class ImageLoader
|
4
|
-
class OriginalImage
|
5
|
-
|
6
|
-
attr_accessor :relative_path
|
7
|
-
|
8
|
-
def initialize(root_dir)
|
9
|
-
@root_dir = root_dir
|
10
|
-
end
|
11
|
-
|
12
|
-
# Full original image path /{root}/some_dir/image.png
|
13
|
-
def full_path
|
14
|
-
File.join(@root_dir, relative_path)
|
15
|
-
end
|
16
|
-
|
17
|
-
# Absolute store /{root}/some_dir
|
18
|
-
def dir_path
|
19
|
-
File.dirname( full_path ).to_s
|
20
|
-
end
|
21
|
-
|
22
|
-
# Requested filename /image.png
|
23
|
-
def filename
|
24
|
-
File.basename( full_path ).to_s
|
25
|
-
end
|
26
|
-
|
27
|
-
# Relative file store dir /some_dir/
|
28
|
-
def relative_dir
|
29
|
-
dir_path.gsub(@root_dir, '')
|
30
|
-
end
|
31
|
-
|
32
|
-
# {Bool} original image exist
|
33
|
-
def image_exist?
|
34
|
-
!Dir[full_path + '.*'].empty?
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module LightResizer
|
3
|
-
class ImageLoader
|
4
|
-
class ResizeImage
|
5
|
-
|
6
|
-
STORE_RESIZE_DIR = 'light_resize' #todo move to config option
|
7
|
-
|
8
|
-
attr_accessor :original_filename, :original_relative_dir, :resize_prefix
|
9
|
-
attr_reader :root_dir
|
10
|
-
|
11
|
-
def initialize(root_dir)
|
12
|
-
@root_dir = root_dir
|
13
|
-
end
|
14
|
-
|
15
|
-
def resize_prefix_dir
|
16
|
-
STORE_RESIZE_DIR
|
17
|
-
end
|
18
|
-
|
19
|
-
# Resized file name '150x150_image.png'
|
20
|
-
def filename
|
21
|
-
"#{resize_prefix}_#{original_filename}"
|
22
|
-
end
|
23
|
-
|
24
|
-
# Relative resize image path '/some_dir/resize/150x150_image.png'
|
25
|
-
def relative_path
|
26
|
-
File.join(original_relative_dir, STORE_RESIZE_DIR, filename)
|
27
|
-
end
|
28
|
-
|
29
|
-
# Absolute image path
|
30
|
-
def full_path
|
31
|
-
File.join(@root_dir, relative_path)
|
32
|
-
end
|
33
|
-
|
34
|
-
# Absolute resize path {root}/some_dir/resize
|
35
|
-
def dir_path
|
36
|
-
File.dirname full_path
|
37
|
-
end
|
38
|
-
|
39
|
-
# {Bool} returns true if resize dir exist?
|
40
|
-
def dir_exist?
|
41
|
-
Dir.exist? dir_path
|
42
|
-
end
|
43
|
-
|
44
|
-
# {Bool} returns true if resize image exist?
|
45
|
-
def image_exist?
|
46
|
-
File.exist? full_path
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module LightResizer
|
3
|
-
class Middleware
|
4
|
-
class Path
|
5
|
-
|
6
|
-
PREFIX_REGEXP = /^[0-9]+x[0-9]+(_crop|_convert)*_/
|
7
|
-
|
8
|
-
attr_reader :request_path
|
9
|
-
|
10
|
-
def request_path=(path)
|
11
|
-
@request_path = path
|
12
|
-
@segments = nil
|
13
|
-
end
|
14
|
-
|
15
|
-
# {Bool} returns true if request path begins with 'image'
|
16
|
-
def image_path?
|
17
|
-
request_dir.end_with?('light_resize')
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
# {Bool} returns true if image should be croped on resize
|
22
|
-
def crop_path?
|
23
|
-
prefix =~ /crop/
|
24
|
-
end
|
25
|
-
|
26
|
-
# {Bool} returns true if image should be converted on resize
|
27
|
-
def convert_path?
|
28
|
-
prefix =~ /convert/
|
29
|
-
end
|
30
|
-
|
31
|
-
# {String} last part of request – relative path
|
32
|
-
def image_path
|
33
|
-
dir = File.expand_path('..', request_dir)
|
34
|
-
File.join(dir, original_filename)
|
35
|
-
end
|
36
|
-
|
37
|
-
def image_extension
|
38
|
-
File.extname(request_path)
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
# {String} returns prefix of resized image name
|
43
|
-
# 200x200_crop_image.png => 200x200_crop
|
44
|
-
def prefix
|
45
|
-
filename[PREFIX_REGEXP].chop if image_path?
|
46
|
-
end
|
47
|
-
|
48
|
-
def dimensions
|
49
|
-
prefix.split('_').first
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def request_dir
|
55
|
-
File.dirname(request_path)
|
56
|
-
end
|
57
|
-
|
58
|
-
def original_filename
|
59
|
-
filename.gsub(PREFIX_REGEXP, '')
|
60
|
-
end
|
61
|
-
|
62
|
-
def filename
|
63
|
-
File.basename(request_path, '.*')
|
64
|
-
end
|
65
|
-
|
66
|
-
def name_segments
|
67
|
-
@segments ||= request_path.split('/')
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module LightResizer
|
3
|
-
class Middleware
|
4
|
-
class Resizer
|
5
|
-
|
6
|
-
def resize(dimensions, original_path, resize_path, crop, extension, convert)
|
7
|
-
check_resized_dir(resize_path)
|
8
|
-
store_image(dimensions, original_path, resize_path, crop, extension)
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def check_resized_dir(resize_path)
|
14
|
-
dir_name = File.dirname resize_path
|
15
|
-
Dir.mkdir dir_name unless Dir.exist? dir_name
|
16
|
-
#todo permissions?
|
17
|
-
end
|
18
|
-
|
19
|
-
def store_image(dimensions, original_path, resize_path, crop, extension)
|
20
|
-
original_path += '.*'
|
21
|
-
width, height = dimensions.split('x').map &:to_i
|
22
|
-
|
23
|
-
@image = Magick::Image.read(Dir[original_path].first).first
|
24
|
-
@image.format = extension[1..-1]
|
25
|
-
|
26
|
-
# WARNING: DO NOT CHANGE COMMANDS ORDER FOR @IMAGE OBJECT
|
27
|
-
# really, don't do this
|
28
|
-
crop ? crop_image(width, height) : resize_image(width, height)
|
29
|
-
|
30
|
-
@image.write(resize_path + extension)
|
31
|
-
end
|
32
|
-
|
33
|
-
def resize_image(width, height)
|
34
|
-
@image.resize_to_fit!(width, height)
|
35
|
-
@image.background_color = 'Transparent'
|
36
|
-
@image = @image.extent(width, height, (@image.columns - width) / 2, (@image.rows - height) / 2) # does not work with simple gravity
|
37
|
-
end
|
38
|
-
|
39
|
-
def crop_image(width, height)
|
40
|
-
@image.resize_to_fill!(width, height)
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
data/spec/fixtures/sample.jpg
DELETED
Binary file
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe LightResizer::Middleware do
|
4
|
-
let(:app) { double("app") }
|
5
|
-
let(:resizer) { described_class.new(app, ROOT, 'fixtures') }
|
6
|
-
let(:env) { { 'PATH_INFO' => resize_relative_path} }
|
7
|
-
|
8
|
-
let(:resize_full_path) { File.join(ROOT, 'fixtures', resize_relative_path) }
|
9
|
-
|
10
|
-
context 'image path' do
|
11
|
-
after(:each) do
|
12
|
-
File.delete(resize_full_path)
|
13
|
-
end
|
14
|
-
let(:resize_relative_path) { '/light_resize/200x100_sample.png' } #todo depends for many constant
|
15
|
-
|
16
|
-
it 'should approve resize request' do
|
17
|
-
resizer.call(env)
|
18
|
-
|
19
|
-
expect(resizer.resize_request?).to be_truthy
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'should create resized image' do
|
23
|
-
expect(File.exist?(resize_full_path)).to be_falsey
|
24
|
-
resizer.call(env)
|
25
|
-
expect(File.exist?(resize_full_path)).to be_truthy
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'non image path' do
|
30
|
-
let(:resize_relative_path) { 'some/other/path' }
|
31
|
-
|
32
|
-
it 'should call app with right environment' do
|
33
|
-
expect(app).to receive(:call).with(env)
|
34
|
-
resizer.call(env)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe LightResizer::ImageLoader::OriginalImage do
|
4
|
-
|
5
|
-
let(:root_dir) { File.join(ROOT, 'fixtures') }
|
6
|
-
let(:store_dir) { '/some/dir' }
|
7
|
-
let(:filename) { 'image' }
|
8
|
-
let(:image_full_path) { File.join(root_dir, store_dir, filename) }
|
9
|
-
let(:original_image) { described_class.new root_dir }
|
10
|
-
|
11
|
-
before(:each) do
|
12
|
-
original_image.relative_path = File.join(store_dir, filename)
|
13
|
-
end
|
14
|
-
|
15
|
-
context 'path methods' do
|
16
|
-
|
17
|
-
it { expect(original_image.send(:full_path)).to eq(image_full_path) }
|
18
|
-
|
19
|
-
it { expect(original_image.send(:dir_path)).to eq( File.join(root_dir, store_dir) ) }
|
20
|
-
|
21
|
-
it { expect(original_image.send(:filename)).to eq( filename ) }
|
22
|
-
|
23
|
-
it { expect(original_image.send(:relative_dir)).to eq(store_dir) }
|
24
|
-
|
25
|
-
it { expect(original_image.send(:image_exist?)).to eq(false) }
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe LightResizer::ImageLoader::ResizeImage do
|
4
|
-
|
5
|
-
let(:root_dir) { File.join(ROOT, 'fixtures') }
|
6
|
-
let(:store_dir) { '/some/dir' }
|
7
|
-
let(:original_filename) { 'image.png' }
|
8
|
-
let(:resized_prefix) { '20x20' }
|
9
|
-
let(:resized_image) { described_class.new root_dir }
|
10
|
-
|
11
|
-
before(:each) do
|
12
|
-
resized_image.original_filename = original_filename
|
13
|
-
resized_image.original_relative_dir = store_dir
|
14
|
-
resized_image.resize_prefix = resized_prefix
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'path methods' do
|
18
|
-
|
19
|
-
it {
|
20
|
-
expected_filename = [resized_prefix, '_', original_filename].join('')
|
21
|
-
expect(resized_image.send(:filename)).to eq( expected_filename )
|
22
|
-
}
|
23
|
-
|
24
|
-
it {
|
25
|
-
expected_path = File.join(store_dir, 'light_resize', resized_image.filename)
|
26
|
-
expect(resized_image.send(:relative_path)).to eq( expected_path )
|
27
|
-
}
|
28
|
-
|
29
|
-
it {
|
30
|
-
expected_path = File.join(root_dir, resized_image.relative_path )
|
31
|
-
expect(resized_image.send(:full_path)).to eq( expected_path )
|
32
|
-
}
|
33
|
-
|
34
|
-
it {
|
35
|
-
expected_path = File.join(root_dir, store_dir, 'light_resize' )
|
36
|
-
expect(resized_image.send(:dir_path)).to eq( expected_path )
|
37
|
-
}
|
38
|
-
|
39
|
-
it { expect(resized_image.send(:dir_exist?)).to eq(false) }
|
40
|
-
|
41
|
-
it { expect(resized_image.send(:image_exist?)).to eq(false) }
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe LightResizer::ImageLoader do
|
4
|
-
|
5
|
-
let(:root_dir) { File.join(ROOT, 'fixtures') }
|
6
|
-
let(:image_loader) { described_class.new root_dir }
|
7
|
-
|
8
|
-
before(:each) do
|
9
|
-
image_loader.original_image_path = '/some/dir/image.png'
|
10
|
-
image_loader.resize_prefix = '20x20'
|
11
|
-
end
|
12
|
-
|
13
|
-
context 'image loader methods' do
|
14
|
-
|
15
|
-
it { expect(image_loader.send(:original_image_exist?)).to eq(false) }
|
16
|
-
|
17
|
-
it { expect(image_loader.send(:resized_image_relative_path)).to eq('/some/dir/light_resize/20x20_image.png') }
|
18
|
-
|
19
|
-
it { expect(image_loader.send(:resized_image_exist?)).to eq(false) }
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe LightResizer::Middleware::Path do
|
4
|
-
|
5
|
-
let(:path) { described_class.new }
|
6
|
-
|
7
|
-
context 'valid path methods' do
|
8
|
-
|
9
|
-
before(:each) do
|
10
|
-
path.request_path = '/some_dir/light_resize/150x150_image.png'
|
11
|
-
end
|
12
|
-
|
13
|
-
it { expect(path.image_path?).to be_truthy }
|
14
|
-
|
15
|
-
it { expect(path.image_path).to eq('/some_dir/image') }
|
16
|
-
|
17
|
-
it { expect(path.image_extension).to eq('.png') }
|
18
|
-
|
19
|
-
it { expect(path.dimensions).to eq('150x150') }
|
20
|
-
|
21
|
-
it { expect(path.prefix).to eq('150x150') }
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
context 'resize with crop' do
|
26
|
-
before(:each) do
|
27
|
-
path.request_path = '/some_dir/light_resize/150x150_crop_convert_image.png'
|
28
|
-
end
|
29
|
-
|
30
|
-
it { expect(path.crop_path?).to be_truthy }
|
31
|
-
|
32
|
-
it { expect(path.convert_path?).to be_truthy }
|
33
|
-
|
34
|
-
it { expect(path.image_path).to eq('/some_dir/image') }
|
35
|
-
|
36
|
-
it { expect(path.image_extension).to eq('.png') }
|
37
|
-
|
38
|
-
it { expect(path.dimensions).to eq('150x150') }
|
39
|
-
|
40
|
-
it { expect(path.prefix).to eq('150x150_crop_convert') }
|
41
|
-
end
|
42
|
-
|
43
|
-
context 'wrong path methods' do
|
44
|
-
|
45
|
-
before(:each) do
|
46
|
-
path.request_path = '/asdada/123acad/some_dir/imdage.png'
|
47
|
-
end
|
48
|
-
|
49
|
-
it { expect(path.image_path?).to be_falsey }
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|