bcms_thumbnail 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -34,6 +34,10 @@ The above, but only for images with a certain tag? Oh, hell yes:
34
34
 
35
35
  Note: The code above is yucky. But cool.
36
36
 
37
+ You can also create square thumbnails via the "thumbnail_square" helper. You should only pass a single integer in for geometry when create a square thumbnail.
38
+ <% image = ImageBlock.find(:first) %>
39
+ <%= image_tag(thumbnail_square(image,'125')) %>
40
+
37
41
  == Geometries
38
42
 
39
43
  ImageMagick will preserve aspect ratio with all the geometries below:
@@ -45,10 +49,15 @@ ImageMagick will preserve aspect ratio with all the geometries below:
45
49
 
46
50
  More info on ImageMagick geometry here: http://www.imagemagick.org/www/command-line-processing.html#geometry
47
51
 
52
+ Remember that "thumbnail_square" only requires a single integer for its geometry string.
53
+
54
+ == Gotchas
55
+
56
+ * Thumbnails are generated when the helper method is called. If you clear the thumbnail cache and don't clear the page cache right afterwards, you may have cached pages calling for thumbnails that aren't regenerated - because the helper method call creates the image. Be sure to clear your page cache after clearing your thumbnail cache and you'll be fine.
57
+ * Speaking of caches - this does nothing to automatically clean up old cached thumbnails from images that've changed or that've been deleted. If you find that these old thumbnails are eating up too much space, Use the rake task to clear out the cache. Be sure to follow it with a page cache flush.
58
+
48
59
  == Todo
49
60
 
50
- * Decache thumbnails when image attachments change via a monkey-patch on the bcms core attachment features.
51
- * Hook image de-caching into the page de-caching control built into the bcms backend.
52
61
  * Beef up the thumbnail helper to expose more mini_magick features.
53
62
  * Better error reporting and sanity checking.
54
63
 
@@ -5,8 +5,20 @@ module ActionView
5
5
  module_function
6
6
 
7
7
  def thumbnail(attachment_obj,geometry = '100x100')
8
+ thumbnail_engine(attachment_obj,geometry,false)
9
+ end
10
+
11
+ def thumbnail_square(attachment_obj,geometry = '100')
12
+ fixed_geometry = geometry
13
+ if geometry.match(/\D/)
14
+ fixed_geometry = geometry.split(/\D/)[0]
15
+ end
16
+ thumbnail_engine(attachment_obj,geometry,true)
17
+ end
18
+
19
+ def thumbnail_engine(attachment_obj,geometry = '100x100',square = false)
8
20
  if ! attachment_obj.blank? && attachment_obj.respond_to?('attachment') && ['jpg','png','gif','bmp'].include?(attachment_obj.attachment.file_extension.downcase)
9
- thumbnail_location = "/bcms_thumbnail_cache/#{geometry}/#{attachment_obj.attachment.file_location.gsub(/[\\\/]/,'-')}.jpg"
21
+ thumbnail_location = "/bcms_thumbnail_cache/#{geometry}/#{attachment_obj.attachment.file_location.gsub(/[\\\/]/,'-')}#{(square) ? '-square' : ''}.jpg"
10
22
  if ! File.exists?("#{RAILS_ROOT}/public#{thumbnail_location}")
11
23
  if ! File.exists?("#{RAILS_ROOT}/public/bcms_thumbnail_cache/#{geometry}")
12
24
  FileUtils.mkdir_p("#{RAILS_ROOT}/public/bcms_thumbnail_cache/#{geometry}")
@@ -14,7 +26,18 @@ module ActionView
14
26
  FileUtils.chmod 0755, "#{RAILS_ROOT}/public/bcms_thumbnail_cache/#{geometry}"
15
27
  end
16
28
  image = MiniMagick::Image.from_file("#{RAILS_ROOT}/tmp/uploads/#{attachment_obj.attachment.file_location}")
17
- image.resize geometry
29
+ if square
30
+ if image[:width] < image[:height]
31
+ remove = ((image[:height] - image[:width])/2).round
32
+ image.shave "0x#{remove}"
33
+ elsif image[:width] > image[:height]
34
+ remove = ((image[:width] - image[:height])/2).round
35
+ image.shave "#{remove}x0"
36
+ end
37
+ image.resize "#{geometry}x#{geometry}"
38
+ else
39
+ image.resize geometry
40
+ end
18
41
  image.write("#{RAILS_ROOT}/public#{thumbnail_location}")
19
42
  FileUtils.chmod 0644, "#{RAILS_ROOT}/public#{thumbnail_location}"
20
43
  URI::escape(thumbnail_location)
@@ -26,5 +49,6 @@ module ActionView
26
49
  '/image-not-found'
27
50
  end
28
51
  end
52
+
29
53
  end
30
54
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 3
9
- version: 1.0.3
8
+ - 4
9
+ version: 1.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dan Collis-Puro
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-25 00:00:00 -04:00
17
+ date: 2010-05-09 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency