| Class | Paperclip::Thumbnail |
| In: |
lib/paperclip/thumbnail.rb
|
| Parent: | Processor |
Handles thumbnailing images that are uploaded.
| convert_options | [RW] | |
| current_geometry | [RW] | |
| format | [RW] | |
| target_geometry | [RW] | |
| whiny | [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 is true (which it is, by default. If convert_options is set, the options will be appended to the convert command upon image conversion
# File lib/paperclip/thumbnail.rb, line 13
13: def initialize file, options = {}, attachment = nil
14: super
15: geometry = options[:geometry]
16: @file = file
17: @crop = geometry[-1,1] == '#'
18: @target_geometry = Geometry.parse geometry
19: @current_geometry = Geometry.from_file @file
20: @convert_options = options[:convert_options]
21: @whiny = options[:whiny].nil? ? true : options[:whiny]
22: @format = options[:format]
23:
24: @current_format = File.extname(@file.path)
25: @basename = File.basename(@file.path, @current_format)
26: end
Returns true if the target_geometry is meant to crop.
# File lib/paperclip/thumbnail.rb, line 29
29: def crop?
30: @crop
31: end
Performs the conversion of the file into a thumbnail. Returns the Tempfile that contains the new image.
# File lib/paperclip/thumbnail.rb, line 40
40: def make
41: src = @file
42: dst = Tempfile.new([@basename, @format].compact.join("."))
43: dst.binmode
44:
45: command = "\"\#{ File.expand_path(src.path) }[0]\"\n\#{ transformation_command }\n\"\#{ File.expand_path(dst.path) }\"\n"
46:
47: begin
48: success = Paperclip.run("convert", command.gsub(/\s+/, " "))
49: rescue PaperclipCommandLineError
50: raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
51: end
52:
53: dst
54: end
Returns the command ImageMagick‘s convert needs to transform the image into the thumbnail.
# File lib/paperclip/thumbnail.rb, line 63
63: def transformation_command
64: scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
65: trans = "-resize \"#{scale}\""
66: trans << " -crop \"#{crop}\" +repage" if crop
67: trans << " #{convert_options}" if convert_options?
68: trans
69: end