| Class | WebThumbnailer |
| In: |
lib/cacher/webthumbnailer.rb
|
| Parent: | Cacher |
| SIZES | = | { 'normal' => '128x128', 'large' => '256x256', 'tiny' => '32x24' |
| SIZE_NAMES | = | SIZES.invert |
| CacheDirs | = | [File.join(ENV['HOME'] || '/tmp', '.thumbnails'), '/tmp/thumbnails'] |
| sizename | [RW] | |
| x | [RW] | |
| y | [RW] |
# File lib/cacher/webthumbnailer.rb, line 16
16: def initialize(size)
17: if !/x/.match(size)
18: size = SIZES[size]
19: end
20: self.x, self.y = size.split('x').map { |e| e.to_i }
21: self.sizename = SIZE_NAMES[size] || size
22: super(CacheDirs)
23: FileUtils.mkdir_p(cachedir)
24: end
# File lib/cacher/webthumbnailer.rb, line 30
30: def cachedir
31: File.join(super, sizename)
32: end
# File lib/cacher/webthumbnailer.rb, line 26
26: def mangle(filename)
27: super('file://' + filename) + '.jpg'
28: end
# File lib/cacher/webthumbnailer.rb, line 34
34: def thumbnail(file)
35: cached(file) do
36: jpeg = File.open(file) do |f|
37: f.read(4) == 'JFIF'
38: end
39: if jpeg
40: e = Epeg.new(file)
41: ox,oy = e.size
42: scale = [1.0 / [(ox / x), 1].max, 1.0 / [(oy / y), 1].max].min
43: dx = (ox * scale).floor
44: dy = (oy * scale).floor
45: e.set_output_size(dx,dy)
46: e.finish
47: else
48: img = Magick::Image.read(file).first
49: img.change_geometry!("#{x}x#{y}") { |cols, rows| img.thumbnail! cols, rows }
50: img.format = 'JPEG'
51: img.to_blob
52: end
53: end
54: end