Handles thumbnailing images that are uploaded.
| [RW] | convert_options | |
| [RW] | current_geometry | |
| [RW] | format | |
| [RW] | target_geometry | |
| [RW] | whiny |
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
[ show source ]
# File lib/data_base/attachment/thumbnail.rb, line 14
14: def initialize(file, options = {})
15: super
16: geometry = options[:geometry]
17: @file = file
18: @crop = geometry[-1,1] == '#'
19: @target_geometry = Geometry.parse geometry
20: @current_geometry = Geometry.from_file @file
21: @convert_options = options[:convert_options]
22: @whiny = options[:whiny].nil? ? true : options[:whiny]
23: @format = options[:format]
24:
25: @current_format = File.extname(@file.path)
26: @basename = File.basename(@file.path, @current_format)
27: end
Returns true if the image is meant to make use of additional convert options.
[ show source ]
# File lib/data_base/attachment/thumbnail.rb, line 35
35: def convert_options?
36: not @convert_options.blank?
37: end
Returns true if the target_geometry is meant to crop.
[ show source ]
# File lib/data_base/attachment/thumbnail.rb, line 30
30: def crop?
31: @crop
32: end
Performs the conversion of the file into a thumbnail. Returns the Tempfile that contains the new image.
[ show source ]
# File lib/data_base/attachment/thumbnail.rb, line 41
41: def make
42: src = @file
43: dst = Tempfile.new([@basename, @format].compact.join("."))
44: dst.binmode
45:
46: command = "\"\#{ File.expand_path(src.path) }[0]\"\n\#{ transformation_command }\n\"\#{ File.expand_path(dst.path) }\"\n"
47:
48: begin
49: success = Attachment.run("convert", command.gsub(/\s+/, " "))
50: rescue AttachmentCommandLineError
51: raise AttachmentError, "There was an error processing the thumbnail for #{@basename}" if @whiny
52: end
53:
54: dst
55: end
Returns the command ImageMagick‘s convert needs to transform the image into the thumbnail.
[ show source ]
# File lib/data_base/attachment/thumbnail.rb, line 64
64: def transformation_command
65: scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
66: trans = "-resize \"#{scale}\""
67: trans << " -crop \"#{crop}\" +repage" if crop
68: trans << " #{convert_options}" if convert_options?
69: trans
70: end