refinerycms-images 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/app/models/image.rb CHANGED
@@ -66,6 +66,47 @@ class Image < ActiveRecord::Base
66
66
  end
67
67
  end
68
68
 
69
+ # Intelligently works out dimensions for a thumbnail of this image based on the Dragonfly geometry string.
70
+ def thumbnail_dimensions(geometry)
71
+ geometry = geometry.to_s
72
+ width = original_width = self.image_width.to_f
73
+ height = original_height = self.image_height.to_f
74
+ geometry_width, geometry_height = geometry.to_s.split(%r{\#{1,2}|\+|>|!|x}im)[0..1].map(&:to_f)
75
+ if (original_width * original_height > 0) && geometry =~ ::Dragonfly::ImageMagick::Processor::THUMB_GEOMETRY
76
+ if geometry =~ ::Dragonfly::ImageMagick::Processor::RESIZE_GEOMETRY
77
+ if geometry !~ %r{\d+x\d+>} || (geometry =~ %r{\d+x\d+>} && (width > geometry_width.to_f || height > geometry_height.to_f))
78
+ # Try scaling with width factor first. (wf = width factor)
79
+ wf_width = (original_width * (geometry_width / width)).ceil
80
+ wf_height = (original_height * (geometry_width / width)).ceil
81
+
82
+ # Scale with height factor (hf = height factor)
83
+ hf_width = (original_width * (geometry_height / height)).ceil
84
+ hf_height = (original_height * (geometry_height / height)).ceil
85
+
86
+ # Take the highest value that doesn't exceed either axis limit.
87
+ use_wf = wf_width <= geometry_width && wf_height <= geometry_height
88
+ if use_wf && hf_width <= geometry_width && hf_height <= geometry_height
89
+ use_wf = wf_width * wf_height > hf_width * hf_height
90
+ end
91
+
92
+ if use_wf
93
+ width = wf_width
94
+ height = wf_height
95
+ else
96
+ width = hf_width
97
+ height = hf_height
98
+ end
99
+ end
100
+ else
101
+ # cropping
102
+ width = geometry_width
103
+ height = geometry_height
104
+ end
105
+ end
106
+
107
+ {:width => width.to_i, :height => height.to_i}
108
+ end
109
+
69
110
  # Returns a titleized version of the filename
70
111
  # my_file.jpg returns My File
71
112
  def title
@@ -8,11 +8,11 @@
8
8
  <%
9
9
  @images.each do |image|
10
10
  thumbnail_urls = {
11
- :"data-original" => image.url,
12
- :"data-grid" => image.thumbnail('135x135#c').url
11
+ :"data-original" => compute_public_path(image.url, ''),
12
+ :"data-grid" => compute_public_path(image.thumbnail('135x135#c').url, '')
13
13
  }
14
14
  ::Image.user_image_sizes.sort_by{|key,geometry| geometry}.each do |size, pixels|
15
- thumbnail_urls[:"data-#{size.to_s.parameterize}"] = image.thumbnail(pixels).url
15
+ thumbnail_urls[:"data-#{size.to_s.parameterize}"] = compute_public_path(image.thumbnail(pixels).url, '')
16
16
  end
17
17
  -%>
18
18
  <li<%= " class='selected'" if @image_id == image.id %>>
@@ -3,10 +3,10 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = %q{refinerycms-images}
6
- s.version = %q{1.0.3}
6
+ s.version = %q{1.0.4}
7
7
  s.summary = %q{Images engine for Refinery CMS}
8
8
  s.description = %q{Handles all image upload and processing functionality in Refinery CMS.}
9
- s.date = %q{2011-06-23}
9
+ s.date = %q{2011-08-11}
10
10
  s.email = %q{info@refinerycms.com}
11
11
  s.homepage = %q{http://refinerycms.com}
12
12
  s.rubyforge_project = %q{refinerycms}
@@ -96,7 +96,7 @@ Gem::Specification.new do |s|
96
96
  'spec/uploads/beach.jpeg'
97
97
  ]
98
98
 
99
- s.add_dependency 'refinerycms-core', '= 1.0.3'
99
+ s.add_dependency 'refinerycms-core', '= 1.0.4'
100
100
  s.add_dependency 'dragonfly', '~> 0.9.0'
101
101
  s.add_dependency 'rack-cache', '>= 0.5.3'
102
102
  end
@@ -69,4 +69,47 @@ describe Image do
69
69
  end
70
70
  end
71
71
 
72
+ # The sample image has dimensions 500x375
73
+ describe '#thumbnail_dimensions returns correctly with' do
74
+ it 'nil' do
75
+ image.thumbnail_dimensions(nil).should == { :width => 500, :height => 375 }
76
+ end
77
+
78
+ it '200x200#ne' do
79
+ image.thumbnail_dimensions('200x200#ne').should == { :width => 200, :height => 200 }
80
+ end
81
+
82
+ it '100x150#c' do
83
+ image.thumbnail_dimensions('100x150#c').should == { :width => 100, :height => 150 }
84
+ end
85
+
86
+ it '250x250>' do
87
+ image.thumbnail_dimensions('250x250>').should == { :width => 250, :height => 188 }
88
+ end
89
+
90
+ it '600x375>' do
91
+ image.thumbnail_dimensions('600x375>').should == { :width => 500, :height => 375 }
92
+ end
93
+
94
+ it '100x475>' do
95
+ image.thumbnail_dimensions('100x475>').should == { :width => 100, :height => 75 }
96
+ end
97
+
98
+ it '100x150' do
99
+ image.thumbnail_dimensions('100x150').should == { :width => 100, :height => 75 }
100
+ end
101
+
102
+ it '200x150' do
103
+ image.thumbnail_dimensions('200x150').should == { :width => 200, :height => 150 }
104
+ end
105
+
106
+ it '300x150' do
107
+ image.thumbnail_dimensions('300x150').should == { :width => 200, :height => 150 }
108
+ end
109
+
110
+ it '5x5' do
111
+ image.thumbnail_dimensions('5x5').should == { :width => 5, :height => 4 }
112
+ end
113
+ end
114
+
72
115
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refinerycms-images
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 3
10
- version: 1.0.3
9
+ - 4
10
+ version: 1.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Resolve Digital
@@ -19,7 +19,7 @@ autorequire:
19
19
  bindir: bin
20
20
  cert_chain: []
21
21
 
22
- date: 2011-06-23 00:00:00 Z
22
+ date: 2011-08-11 00:00:00 Z
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency
25
25
  name: refinerycms-core
@@ -29,12 +29,12 @@ dependencies:
29
29
  requirements:
30
30
  - - "="
31
31
  - !ruby/object:Gem::Version
32
- hash: 17
32
+ hash: 31
33
33
  segments:
34
34
  - 1
35
35
  - 0
36
- - 3
37
- version: 1.0.3
36
+ - 4
37
+ version: 1.0.4
38
38
  type: :runtime
39
39
  version_requirements: *id001
40
40
  - !ruby/object:Gem::Dependency
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  requirements: []
164
164
 
165
165
  rubyforge_project: refinerycms
166
- rubygems_version: 1.8.5
166
+ rubygems_version: 1.8.6
167
167
  signing_key:
168
168
  specification_version: 3
169
169
  summary: Images engine for Refinery CMS