| Class | Paperclip::Thumbnail |
| In: |
lib/paperclip/thumbnail.rb
|
| Parent: | Object |
Handles thumbnailing images that are uploaded.
| current_geometry | [RW] | |
| file | [RW] | |
| format | [RW] | |
| target_geometry | [RW] | |
| whiny_thumbnails | [RW] |
Creates a Thumbnail object set to work on the file given. It will attempt to transform the image into one defined by target_geometry which is a "WxH"-style string. format will be inferred from the file unless specified. Thumbnail creation will raise no errors unless whiny_thumbnails is true (which it is, by default.
# File lib/paperclip/thumbnail.rb, line 12
12: def initialize file, target_geometry, format = nil, whiny_thumbnails = true
13: @file = file
14: @crop = target_geometry[-1,1] == '#'
15: @target_geometry = Geometry.parse target_geometry
16: @current_geometry = Geometry.from_file file
17: @whiny_thumbnails = whiny_thumbnails
18:
19: @current_format = File.extname(@file.path)
20: @basename = File.basename(@file.path, @current_format)
21:
22: @format = format
23: end
Returns true if the target_geometry is meant to crop.
# File lib/paperclip/thumbnail.rb, line 32
32: def crop?
33: @crop
34: end
Performs the conversion of the file into a thumbnail. Returns the Tempfile that contains the new image.
# File lib/paperclip/thumbnail.rb, line 38
38: def make
39: src = @file
40: dst = Tempfile.new([@basename, @format].compact.join("."))
41: dst.binmode
42:
43: command = "\#{ Paperclip.path_for_command('convert') }\n\"\#{ File.expand_path(src.path) }\"\n\#{ transformation_command }\n\"\#{ File.expand_path(dst.path) }\"\n"
44: success = system(command.gsub(/\s+/, " "))
45:
46: if success && $?.exitstatus != 0 && @whiny_thumbnails
47: raise PaperclipError, "There was an error processing this thumbnail"
48: end
49:
50: dst
51: end
Returns the command ImageMagick‘s convert needs to transform the image into the thumbnail.
# File lib/paperclip/thumbnail.rb, line 61
61: def transformation_command
62: scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
63: trans = "-scale \"#{scale}\""
64: trans << " -crop \"#{crop}\" +repage" if crop
65: trans
66: end