light_resizer 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 635a29878155e03c0d5142699597f9e67cd89129
4
- data.tar.gz: aa311b1cc54b289522ad3d517f782c0335310506
3
+ metadata.gz: bc5c78b129e5231781ba262d6dac0f7752edd977
4
+ data.tar.gz: 838eb313c7c7cc204308ae399ab14ed2442df8c7
5
5
  SHA512:
6
- metadata.gz: c9c9e118c322f0ec895d1277384c61db52f61b38a4a484d0c9bd0f4a42014641de038991186b2811d4cae0d4ed3d75865610fafd32a9133c3d2114f41bc43208
7
- data.tar.gz: 2825c896f1e8b5c6adb81bf2e2c26fb2fae4a5192e96a4be7e9163222cd7fadc810e78a4a27ecd22bee133fb219cdd33c8156b756a35f847ecbea2a532df8ef0
6
+ metadata.gz: 1a146c0e1021f487ae735be544bee6bd1191bd2e83a1a6ab4b339fa5dc288a2268dd813b33c5d4a4176060ae5f36e964e113a2b58610fbcc3a12f7a98b6e742e
7
+ data.tar.gz: 69c813f29b65347c5c73e309a7617e2db3b747d000f785d54bfffbb35dd3a80d4d882f737e6df409f3ef2faada0e0e51c58ac29ce5d200b6ba0e2d4f3caa5fc1
data/README.md CHANGED
@@ -4,4 +4,41 @@ light-resizer
4
4
  [![Build Status](https://travis-ci.org/Rademade/light_resizer.svg?branch=master)](https://travis-ci.org/Rademade/light_resizer)
5
5
 
6
6
  Rack middleware for image resizer. Can be integrated with CarrierWave.
7
+ Instalation
8
+ ===========
9
+ Install gem:
10
+
11
+ $ gem install light_resizer
12
+
13
+ And set it in config:
14
+ ```ruby
15
+ config.middleware.insert_before(Rack::Sendfile, LightResizer::Middleware, Rails.root)
16
+ ```
17
+
18
+
19
+ Usage
20
+ =====
21
+ For example we have image in public folder by next url:
22
+
23
+ >example.com/images/kitten.jpg
24
+
25
+ Url of resized image will be:
26
+
27
+ >example.com/images/**light_resize/100x150_kitten.jpg**
28
+
29
+ light_resizer will find */public/images/kitten.jpg* and create *light_resize* with resized to size of 100x150px image.
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
+ Default resize doesn't crop image and fill new space with transparent background.
38
+
39
+ To crop image use '*crop*' in url:
40
+ >example.com/images/light_resize/100x150_crop_kitten.png
41
+
42
+
43
+
7
44
 
@@ -1,22 +1,24 @@
1
1
  module LightResizer
2
2
  module CarrierWaveResize
3
- def resize(width, height)
3
+ def resize_without_crop(width, height)
4
4
  filename_prefix = "#{width}x#{height}_"
5
- resized_image_path(filename_prefix)
5
+ resized_image_path(filename_prefix, 'png')
6
6
  end
7
7
 
8
- def resize_crop(width, height)
8
+ alias_method :resize, :resize_without_crop
9
+
10
+ def resize_with_crop(width, height)
9
11
  filename_prefix = "#{width}x#{height}_crop_"
10
12
  resized_image_path(filename_prefix)
11
13
  end
12
14
 
13
15
  private
14
- def resized_image_path(prefix)
16
+ def resized_image_path(prefix, extension = nil)
15
17
  if url
16
18
  path = File.dirname(url)
17
- filename = File.basename(url)
18
- new_filname = prefix + filename
19
-
19
+ filename = File.basename(url, '.*')
20
+ new_filname = prefix + filename + '.png'
21
+
20
22
  File.join(path, 'light_resize', new_filname)
21
23
  end
22
24
  end
@@ -31,7 +31,7 @@ module LightResizer
31
31
 
32
32
  # {Bool} original image exist
33
33
  def image_exist?
34
- File.exist? full_path
34
+ !Dir[full_path + '.*'].empty?
35
35
  end
36
36
 
37
37
  end
@@ -3,7 +3,7 @@ module LightResizer
3
3
  class Middleware
4
4
  class Path
5
5
 
6
- PREFIX_REGEXP = /^[0-9]+x[0-9]+(_crop)?_/
6
+ PREFIX_REGEXP = /^[0-9]+x[0-9]+(_crop|_convert)*_/
7
7
 
8
8
  attr_reader :request_path
9
9
 
@@ -20,7 +20,12 @@ module LightResizer
20
20
 
21
21
  # {Bool} returns true if image should be croped on resize
22
22
  def crop_path?
23
- prefix.end_with?('crop')
23
+ prefix =~ /crop/
24
+ end
25
+
26
+ # {Bool} returns true if image should be converted on resize
27
+ def convert_path?
28
+ prefix =~ /convert/
24
29
  end
25
30
 
26
31
  # {String} last part of request – relative path
@@ -29,6 +34,10 @@ module LightResizer
29
34
  File.join(dir, original_filename)
30
35
  end
31
36
 
37
+ def image_extension
38
+ File.extname(request_path)
39
+ end
40
+
32
41
 
33
42
  # {String} returns prefix of resized image name
34
43
  # 200x200_crop_image.png => 200x200_crop
@@ -51,13 +60,12 @@ module LightResizer
51
60
  end
52
61
 
53
62
  def filename
54
- File.basename(request_path)
63
+ File.basename(request_path, '.*')
55
64
  end
56
65
 
57
66
  def name_segments
58
67
  @segments ||= request_path.split('/')
59
68
  end
60
-
61
69
  end
62
70
  end
63
71
  end
@@ -3,9 +3,9 @@ module LightResizer
3
3
  class Middleware
4
4
  class Resizer
5
5
 
6
- def resize(dimensions, original_path, resize_path, crop)
6
+ def resize(dimensions, original_path, resize_path, crop, extension, convert)
7
7
  check_resized_dir(resize_path)
8
- store_image(dimensions, original_path, resize_path, crop)
8
+ store_image(dimensions, original_path, resize_path, crop, extension)
9
9
  end
10
10
 
11
11
  private
@@ -15,27 +15,34 @@ module LightResizer
15
15
  #todo permissions?
16
16
  end
17
17
 
18
- def store_image(dimensions, original_path, resize_path, crop)
19
- @image = MiniMagick::Image.open original_path
18
+ def store_image(dimensions, original_path, resize_path, crop, extension)
19
+ original_path += '.*'
20
+ @image = MiniMagick::Image.open Dir[original_path].first
20
21
 
22
+ @extension = extension[1..-1]
23
+
24
+ # WARNING: DO NOT CHANGE COMMANDS ORDER FOR @IMAGE OBJECT
25
+ # realy, don't do this
26
+ @image.format(@extension)
21
27
  crop ? set_crop_options(dimensions) : set_options(dimensions)
22
28
 
23
- @image.write resize_path
29
+ @image.write (resize_path + extension)
24
30
  end
25
31
 
26
32
  def set_options(dimensions)
27
33
  @image.combine_options do |c|
28
- c.resize dimensions
29
- c.unsharp '0x1'
34
+ c.adaptive_resize dimensions
35
+ c.add_command 'quality', '0'
30
36
  c.add_command 'extent', dimensions
31
37
  c.gravity 'center'
38
+ c.background 'transparent'
32
39
  end
33
40
  end
34
41
 
35
42
  def set_crop_options(dimensions)
36
43
  @image.combine_options do |c|
37
- c.resize dimensions+'^'
38
- c.unsharp '0x1'
44
+ c.adaptive_resize dimensions+'^'
45
+ c.add_command 'quality', '0'
39
46
  c.add_command 'extent', dimensions
40
47
  c.gravity 'center'
41
48
  end
@@ -36,7 +36,7 @@ module LightResizer
36
36
 
37
37
  def resize
38
38
  unless @image_loader.resized_image_exist?
39
- @resizer.resize(@path.dimensions, @image_loader.original_path, @image_loader.resize_path, @path.crop_path?)
39
+ @resizer.resize(@path.dimensions, @image_loader.original_path, @image_loader.resize_path, @path.crop_path?, @path.image_extension, @path.convert_path?)
40
40
  end
41
41
  end
42
42
 
@@ -1,3 +1,3 @@
1
1
  module LightResizer
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "rspec"
27
27
  spec.add_development_dependency "coveralls"
28
+ spec.add_development_dependency "pry"
28
29
  end
Binary file
@@ -1,27 +1,37 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe LightResizer::Middleware do
4
- let(:app) { AppEmulator.new }
4
+ let(:app) { double("app") }
5
5
  let(:resizer) { described_class.new(app, ROOT, 'fixtures') }
6
6
  let(:env) { { 'PATH_INFO' => resize_relative_path} }
7
7
 
8
- let(:resize_relative_path) { '/light_resize/150x100_avatar.png' } #todo depends for many constant
9
8
  let(:resize_full_path) { File.join(ROOT, 'fixtures', resize_relative_path) }
10
9
 
11
- after(:each) do
12
- File.delete(resize_full_path)
13
- end
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
14
15
 
15
- it 'should approve resize request' do
16
- resizer.call(env)
16
+ it 'should approve resize request' do
17
+ resizer.call(env)
17
18
 
18
- expect(resizer.resize_request?).to be_true
19
- end
19
+ expect(resizer.resize_request?).to be_true
20
+ end
20
21
 
21
- it 'should create resized image' do
22
- expect(File.exist?(resize_full_path)).to be_false
23
- resizer.call(env)
24
- expect(File.exist?(resize_full_path)).to be_true
22
+ it 'should create resized image' do
23
+ expect(File.exist?(resize_full_path)).to be_false
24
+ resizer.call(env)
25
+ expect(File.exist?(resize_full_path)).to be_true
26
+ end
25
27
  end
26
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
27
37
  end
data/spec/spec_helper.rb CHANGED
@@ -1,18 +1,12 @@
1
1
  require 'coveralls'
2
2
  Coveralls.wear!
3
3
 
4
+ require 'pry'
5
+
4
6
  require 'rack'
5
7
  require 'light_resizer'
6
8
  ROOT = File.dirname(__FILE__)
7
9
 
8
- class AppEmulator
9
- attr_reader :env
10
- def call(env)
11
- @env = env
12
- end
13
- end
14
-
15
-
16
10
  # This file was generated by the `rspec --init` command. Conventionally, all
17
11
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
18
12
  # Require this file using `require "spec_helper"` to ensure that it is only
@@ -4,7 +4,7 @@ describe LightResizer::ImageLoader::OriginalImage do
4
4
 
5
5
  let(:root_dir) { File.join(ROOT, 'fixtures') }
6
6
  let(:store_dir) { '/some/dir' }
7
- let(:filename) { 'image.png' }
7
+ let(:filename) { 'image' }
8
8
  let(:image_full_path) { File.join(root_dir, store_dir, filename) }
9
9
  let(:original_image) { described_class.new root_dir }
10
10
 
@@ -12,7 +12,9 @@ describe LightResizer::Middleware::Path do
12
12
 
13
13
  it { expect(path.image_path?).to be_true }
14
14
 
15
- it { expect(path.image_path).to eq('/some_dir/image.png') }
15
+ it { expect(path.image_path).to eq('/some_dir/image') }
16
+
17
+ it { expect(path.image_extension).to eq('.png') }
16
18
 
17
19
  it { expect(path.dimensions).to eq('150x150') }
18
20
 
@@ -22,16 +24,20 @@ describe LightResizer::Middleware::Path do
22
24
 
23
25
  context 'resize with crop' do
24
26
  before(:each) do
25
- path.request_path = '/some_dir/light_resize/150x150_crop_image.png'
27
+ path.request_path = '/some_dir/light_resize/150x150_crop_convert_image.png'
26
28
  end
27
29
 
28
30
  it { expect(path.crop_path?).to be_true }
29
31
 
30
- it { expect(path.image_path).to eq('/some_dir/image.png') }
32
+ it { expect(path.convert_path?).to be_true }
33
+
34
+ it { expect(path.image_path).to eq('/some_dir/image') }
35
+
36
+ it { expect(path.image_extension).to eq('.png') }
31
37
 
32
38
  it { expect(path.dimensions).to eq('150x150') }
33
39
 
34
- it { expect(path.prefix).to eq('150x150_crop') }
40
+ it { expect(path.prefix).to eq('150x150_crop_convert') }
35
41
  end
36
42
 
37
43
  context 'wrong path methods' do
@@ -3,30 +3,30 @@ require 'spec_helper'
3
3
  describe LightResizer::Middleware::Resizer do
4
4
 
5
5
  let(:resizer) { described_class.new }
6
- let(:image_path) { File.join(ROOT, 'fixtures', 'avatar.png') }
6
+ let(:image_path) { File.join(ROOT, 'fixtures', 'sample') }
7
7
  let(:resize_image_path) { File.join(ROOT, 'fixtures', 'light_resize', image_name) }
8
8
 
9
9
  context 'resize' do
10
10
 
11
11
  after do
12
- File.delete( resize_image_path )
12
+ File.delete( resize_image_path + '.png' )
13
13
  end
14
14
 
15
15
  context 'without crop' do
16
- let(:image_name) { '50x50_avatar.png' }
16
+ let(:image_name) { '50x50_sample' }
17
17
 
18
18
  it 'should create resized image file' do
19
- resizer.resize('50x50', image_path, resize_image_path, false)
20
- expect(File.exist?(resize_image_path)).to eq(true)
19
+ resizer.resize('50x50', image_path, resize_image_path, false, '.png', true)
20
+ expect(File.exist?(resize_image_path+'.png')).to eq(true)
21
21
  end
22
22
  end
23
23
 
24
24
  context 'with crop' do
25
- let(:image_name) { '50x50_crop_avatar.png' }
26
-
25
+ let(:image_name) { '50x50_crop_convert_sample' }
26
+
27
27
  it 'should create resized image file' do
28
- resizer.resize('50x50', image_path, resize_image_path, true)
29
- expect(File.exist?(resize_image_path)).to eq(true)
28
+ resizer.resize('50x50', image_path, resize_image_path, true, '.png', true)
29
+ expect(File.exist?(resize_image_path+'.png')).to eq(true)
30
30
  end
31
31
  end
32
32
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light_resizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Sergienko
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-22 00:00:00.000000000 Z
12
+ date: 2014-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mini_magick
@@ -95,6 +95,20 @@ dependencies:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: pry
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
98
112
  description: ''
99
113
  email:
100
114
  - olol.toor@gmail.com
@@ -121,7 +135,7 @@ files:
121
135
  - lib/light_resizer/middleware/resizer.rb
122
136
  - lib/light_resizer/version.rb
123
137
  - light_resizer.gemspec
124
- - spec/fixtures/avatar.png
138
+ - spec/fixtures/sample.jpg
125
139
  - spec/integration/middleware_spec.rb
126
140
  - spec/spec_helper.rb
127
141
  - spec/unit/image_loader_original_spec.rb
@@ -154,7 +168,7 @@ signing_key:
154
168
  specification_version: 4
155
169
  summary: Tiny rack middleware for image resizing.
156
170
  test_files:
157
- - spec/fixtures/avatar.png
171
+ - spec/fixtures/sample.jpg
158
172
  - spec/integration/middleware_spec.rb
159
173
  - spec/spec_helper.rb
160
174
  - spec/unit/image_loader_original_spec.rb
Binary file