carrierwave_imagevoodoo 0.0.3-java → 0.0.4-java

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.
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .rvmrc
19
+ .ruby-version
data/README.md CHANGED
@@ -6,6 +6,7 @@ CarrierWave support for ImageVoodoo
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
+ gem 'image_voodoo'
9
10
  gem 'carrierwave_imagevoodoo'
10
11
 
11
12
  And then execute:
@@ -14,7 +15,7 @@ And then execute:
14
15
 
15
16
  Or install it yourself as:
16
17
 
17
- $ gem install carrierwave_imagevoodoo
18
+ $ gem install image_voodoo carrierwave_imagevoodoo
18
19
 
19
20
  ## Usage
20
21
 
Binary file
@@ -0,0 +1,38 @@
1
+ require "benchmark"
2
+ require "carrierwave"
3
+
4
+ class CatUploader < CarrierWave::Uploader::Base
5
+ storage :file
6
+
7
+ 100.times do |i|
8
+ version "size#{i}" do
9
+ process resize_to_fit: [200, 200]
10
+ end
11
+ end
12
+ end
13
+
14
+ cat_file = File.new("images/cat.jpg")
15
+
16
+ case RUBY_PLATFORM
17
+ when "java"
18
+ require "../lib/carrierwave_imagevoodoo"
19
+ CatUploader.send :include, CarrierWave::ImageVoodoo
20
+ else
21
+ CatUploader.send :include, CarrierWave::MiniMagick
22
+ end
23
+
24
+ instance = CatUploader.new
25
+
26
+ if File.directory? "uploads"
27
+ FileUtils.rm_r "uploads"
28
+ end
29
+
30
+ puts "STARTED"
31
+ 3.times do
32
+ puts Benchmark.measure {
33
+ instance.store! cat_file
34
+ }
35
+
36
+ FileUtils.rm_r "uploads"
37
+ end
38
+ puts "ENDED"
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
 
19
19
  gem.add_dependency "carrierwave"
20
20
  gem.add_dependency "image_voodoo"
21
+ gem.add_dependency "activesupport", ">= 3.0.0"
21
22
 
22
23
  gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "rspec", ">= 2.12.0"
23
25
  end
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module ImageVoodoo
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -1,4 +1,6 @@
1
- require "carrierwave_imagevoodoo/version"
1
+ require "carrierwave"
2
+ require "image_voodoo"
3
+ require "active_support/concern"
2
4
 
3
5
  module CarrierWave
4
6
  module ImageVoodoo
@@ -43,8 +45,8 @@ module CarrierWave
43
45
 
44
46
  if width != cols or height != rows
45
47
  scale = [width/cols.to_f, height/rows.to_f].min
46
- cols = (scale * (cols + 0.5)).round
47
- rows = (scale * (rows + 0.5)).round
48
+ cols = (scale * cols).round
49
+ rows = (scale * rows).round
48
50
  image.resize cols, rows do |img|
49
51
  yield(img) if block_given?
50
52
  img.save current_path
@@ -53,22 +55,63 @@ module CarrierWave
53
55
  end
54
56
  end
55
57
 
56
- def resize_to_fill width, height
58
+ def resize_to_fill(new_width, new_height)
57
59
  manipulate! do |image|
58
60
  cols = image.width
59
61
  rows = image.height
62
+ width, height = extract_dimensions_for_crop(image.width, image.height, new_width, new_height)
63
+ x_offset, y_offset = extract_placement_for_crop(width, height, new_width, new_height)
60
64
 
61
- if width != cols or height != rows
62
- scale = [width/cols.to_f, height/rows.to_f].max
63
- cols = (scale * (cols + 0.5)).round
64
- rows = (scale * (rows + 0.5)).round
65
- image.resize cols, rows do |img|
66
- yield(img) if block_given?
67
- img.save current_path
65
+ # check if if new dimensions are too small for the new image
66
+ if width < new_width
67
+ width = new_width
68
+ height = (new_width.to_f*(image.height.to_f/image.width.to_f)).round
69
+ elsif height < new_height
70
+ height = new_height
71
+ width = (new_height.to_f*(image.width.to_f/image.height.to_f)).round
72
+ end
73
+
74
+ image.resize( width, height ) do |i2|
75
+
76
+ # check to make sure offset is not negative
77
+ if x_offset < 0
78
+ x_offset = 0
79
+ end
80
+ if y_offset < 0
81
+ y_offset = 0
82
+ end
83
+
84
+ i2.with_crop( x_offset, y_offset, new_width + x_offset, new_height + y_offset) do |file|
85
+ file.save( self.current_path )
68
86
  end
69
87
  end
70
88
  end
71
89
  end
90
+
91
+ private
92
+
93
+ def extract_dimensions(width, height, new_width, new_height, type = :resize)
94
+ aspect_ratio = width.to_f / height.to_f
95
+ new_aspect_ratio = new_width / new_height
96
+
97
+ if (new_aspect_ratio > aspect_ratio) ^ ( type == :crop ) # Image is too wide, the caret is the XOR operator
98
+ new_width, new_height = [ (new_height * aspect_ratio), new_height]
99
+ else #Image is too narrow
100
+ new_width, new_height = [ new_width, (new_width / aspect_ratio)]
101
+ end
102
+
103
+ [new_width, new_height].collect! { |v| v.round }
104
+ end
105
+
106
+ def extract_dimensions_for_crop(width, height, new_width, new_height)
107
+ extract_dimensions(width, height, new_width, new_height, :crop)
108
+ end
109
+
110
+ def extract_placement_for_crop(width, height, new_width, new_height)
111
+ x_offset = (width / 2.0) - (new_width / 2.0)
112
+ y_offset = (height / 2.0) - (new_height / 2.0)
113
+ [x_offset, y_offset].collect! { |v| v.round }
114
+ end
72
115
 
73
116
  #def resize_and_pad width, height, background = :transparent
74
117
  #end
@@ -77,4 +120,4 @@ module CarrierWave
77
120
  yield ::ImageVoodoo.with_bytes file.read
78
121
  end
79
122
  end
80
- end
123
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::ImageVoodoo do
4
+ let :klass do
5
+ Class.new do
6
+ include CarrierWave::ImageVoodoo
7
+ end
8
+ end
9
+
10
+ let :instance do
11
+ klass.new
12
+ end
13
+
14
+ before do
15
+ FileUtils.cp(file_path('cat.jpg'), file_path('cat_copy.jpg'))
16
+ instance.stub(:file).and_return(File.new(file_path('cat_copy.jpg')))
17
+ instance.stub(:current_path).and_return(file_path('cat_copy.jpg'))
18
+ instance.stub(:cached?).and_return true
19
+ end
20
+
21
+ after do
22
+ FileUtils.rm(file_path('cat_copy.jpg'))
23
+ end
24
+
25
+ describe '#resize_to_fit' do
26
+ it "should resize the image to fit within the given dimensions" do
27
+ instance.resize_to_fit(200, 200)
28
+ instance.should have_dimensions(127, 200)
29
+ end
30
+
31
+ it "should not resize an image whose largest side matchesthe given dimensions" do
32
+ instance.resize_to_fit(1000, 1000)
33
+ instance.should have_dimensions(635, 1000)
34
+ end
35
+
36
+ it "should scale the image up if smaller than given dimensions" do
37
+ instance.resize_to_fit(2000, 2000)
38
+ instance.should have_dimensions(1270, 2000)
39
+ end
40
+ end
41
+
42
+ describe '#resize_to_fill' do
43
+ it "should resize the image to exactly the given dimensions" do
44
+ instance.resize_to_fill(200, 200)
45
+ instance.should have_dimensions(200, 200)
46
+ end
47
+
48
+ it "should scale the image up if smaller than given dimensions" do
49
+ instance.resize_to_fill(1000, 1000)
50
+ instance.should have_dimensions(1000, 1000)
51
+ end
52
+ end
53
+ end
Binary file
@@ -0,0 +1,36 @@
1
+ require "carrierwave_imagevoodoo"
2
+
3
+ def file_path( *paths )
4
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
5
+ end
6
+
7
+ class HaveDimensions
8
+ def initialize(width, height)
9
+ @width, @height = width, height
10
+ end
11
+
12
+ def matches?(actual)
13
+ @actual = actual
14
+ # Satisfy expectation here. Return false or raise an error if it's not met.
15
+ image = ::ImageVoodoo.with_bytes File.new(@actual.current_path).read
16
+ @actual_width = image.width
17
+ @actual_height = image.height
18
+ @actual_width == @width && @actual_height == @height
19
+ end
20
+
21
+ def failure_message
22
+ "expected #{@actual.current_path.inspect} to have an exact size of #{@width} by #{@height}, but it was #{@actual_width} by #{@actual_height}."
23
+ end
24
+
25
+ def negative_failure_message
26
+ "expected #{@actual.current_path.inspect} not to have an exact size of #{@width} by #{@height}, but it did."
27
+ end
28
+
29
+ def description
30
+ "have an exact size of #{@width} by #{@height}"
31
+ end
32
+ end
33
+
34
+ def have_dimensions(width, height)
35
+ HaveDimensions.new(width, height)
36
+ end
metadata CHANGED
@@ -1,64 +1,102 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave_imagevoodoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.0.4
5
+ prerelease:
6
6
  platform: java
7
7
  authors:
8
8
  - Tasveer Singh
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-14 00:00:00.000000000 Z
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: carrierwave
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
16
+ version_requirements: !ruby/object:Gem::Requirement
18
17
  requirements:
19
- - - ! '>='
18
+ - - ">="
20
19
  - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
20
+ version: !binary |-
21
+ MA==
25
22
  none: false
23
+ requirement: !ruby/object:Gem::Requirement
26
24
  requirements:
27
- - - ! '>='
25
+ - - ">="
28
26
  - !ruby/object:Gem::Version
29
- version: '0'
27
+ version: !binary |-
28
+ MA==
29
+ none: false
30
+ prerelease: false
31
+ type: :runtime
30
32
  - !ruby/object:Gem::Dependency
31
33
  name: image_voodoo
32
- requirement: !ruby/object:Gem::Requirement
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: !binary |-
39
+ MA==
33
40
  none: false
41
+ requirement: !ruby/object:Gem::Requirement
34
42
  requirements:
35
- - - ! '>='
43
+ - - ">="
36
44
  - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :runtime
45
+ version: !binary |-
46
+ MA==
47
+ none: false
39
48
  prerelease: false
49
+ type: :runtime
50
+ - !ruby/object:Gem::Dependency
51
+ name: activesupport
40
52
  version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.0
41
57
  none: false
58
+ requirement: !ruby/object:Gem::Requirement
42
59
  requirements:
43
- - - ! '>='
60
+ - - ">="
44
61
  - !ruby/object:Gem::Version
45
- version: '0'
62
+ version: 3.0.0
63
+ none: false
64
+ prerelease: false
65
+ type: :runtime
46
66
  - !ruby/object:Gem::Dependency
47
67
  name: rake
48
- requirement: !ruby/object:Gem::Requirement
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: !binary |-
73
+ MA==
49
74
  none: false
75
+ requirement: !ruby/object:Gem::Requirement
50
76
  requirements:
51
- - - ! '>='
77
+ - - ">="
52
78
  - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
79
+ version: !binary |-
80
+ MA==
81
+ none: false
55
82
  prerelease: false
83
+ type: :development
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
56
86
  version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 2.12.0
57
91
  none: false
92
+ requirement: !ruby/object:Gem::Requirement
58
93
  requirements:
59
- - - ! '>='
94
+ - - ">="
60
95
  - !ruby/object:Gem::Version
61
- version: '0'
96
+ version: 2.12.0
97
+ none: false
98
+ prerelease: false
99
+ type: :development
62
100
  description: CarrierWave support for ImageVoodoo
63
101
  email:
64
102
  - taz@zenapsis.com
@@ -66,36 +104,52 @@ executables: []
66
104
  extensions: []
67
105
  extra_rdoc_files: []
68
106
  files:
69
- - .gitignore
107
+ - ".gitignore"
70
108
  - Gemfile
71
109
  - LICENSE
72
110
  - README.md
73
111
  - Rakefile
112
+ - benchmarks/images/cat.jpg
113
+ - benchmarks/minimagick_vs_imagevoodoo.rb
74
114
  - carrierwave_imagevoodoo.gemspec
75
115
  - lib/carrierwave_imagevoodoo.rb
76
116
  - lib/carrierwave_imagevoodoo/version.rb
117
+ - spec/carrierwave_imagevoodoo_spec.rb
118
+ - spec/fixtures/cat.jpg
119
+ - spec/spec_helper.rb
77
120
  homepage: https://github.com/zenapsis/carrierwave_imagevoodoo
78
121
  licenses: []
79
- post_install_message:
122
+ post_install_message:
80
123
  rdoc_options: []
81
124
  require_paths:
82
125
  - lib
83
126
  required_ruby_version: !ruby/object:Gem::Requirement
84
- none: false
85
127
  requirements:
86
- - - ! '>='
128
+ - - ">="
87
129
  - !ruby/object:Gem::Version
88
- version: '0'
89
- required_rubygems_version: !ruby/object:Gem::Requirement
130
+ segments:
131
+ - 0
132
+ version: !binary |-
133
+ MA==
134
+ hash: 2
90
135
  none: false
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
137
  requirements:
92
- - - ! '>='
138
+ - - ">="
93
139
  - !ruby/object:Gem::Version
94
- version: '0'
140
+ segments:
141
+ - 0
142
+ version: !binary |-
143
+ MA==
144
+ hash: 2
145
+ none: false
95
146
  requirements: []
96
- rubyforge_project:
97
- rubygems_version: 1.8.21
98
- signing_key:
147
+ rubyforge_project:
148
+ rubygems_version: 1.8.24
149
+ signing_key:
99
150
  specification_version: 3
100
151
  summary: A simple CarrierWave processor utilizing ImageVoodoo for processing
101
- test_files: []
152
+ test_files:
153
+ - spec/carrierwave_imagevoodoo_spec.rb
154
+ - spec/fixtures/cat.jpg
155
+ - spec/spec_helper.rb