Class Paperclip::Thumbnail
In: lib/paperclip/thumbnail.rb
Parent: Processor

Handles thumbnailing images that are uploaded.

Methods

Attributes

convert_options  [RW] 
current_geometry  [RW] 
format  [RW] 
source_file_options  [RW] 
target_geometry  [RW] 
whiny  [RW] 

Public Class methods

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

[Source]

    # File lib/paperclip/thumbnail.rb, line 13
13:     def initialize file, options = {}, attachment = nil
14:       super
15: 
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:       @source_file_options = options[:source_file_options]
22:       @convert_options     = options[:convert_options]
23:       @whiny               = options[:whiny].nil? ? true : options[:whiny]
24:       @format              = options[:format]
25: 
26:       @source_file_options = @source_file_options.split(/\s+/) if @source_file_options.respond_to?(:split)
27:       @convert_options     = @convert_options.split(/\s+/)     if @convert_options.respond_to?(:split)
28: 
29:       @current_format      = File.extname(@file.path)
30:       @basename            = File.basename(@file.path, @current_format)
31: 
32:     end

Public Instance methods

Returns true if the image is meant to make use of additional convert options.

[Source]

    # File lib/paperclip/thumbnail.rb, line 40
40:     def convert_options?
41:       !@convert_options.nil? && !@convert_options.empty?
42:     end

Returns true if the target_geometry is meant to crop.

[Source]

    # File lib/paperclip/thumbnail.rb, line 35
35:     def crop?
36:       @crop
37:     end

Performs the conversion of the file into a thumbnail. Returns the Tempfile that contains the new image.

[Source]

    # File lib/paperclip/thumbnail.rb, line 46
46:     def make
47:       src = @file
48:       dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
49:       dst.binmode
50: 
51:       begin
52:         parameters = []
53:         parameters << source_file_options
54:         parameters << ":source"
55:         parameters << transformation_command
56:         parameters << convert_options
57:         parameters << ":dest"
58: 
59:         parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
60: 
61:         success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
62:       rescue PaperclipCommandLineError => e
63:         raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
64:       end
65: 
66:       dst
67:     end

Returns the command ImageMagick‘s convert needs to transform the image into the thumbnail.

[Source]

    # File lib/paperclip/thumbnail.rb, line 71
71:     def transformation_command
72:       scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
73:       trans = []
74:       trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
75:       trans << "-crop" << %["#{crop}"] << "+repage" if crop
76:       trans
77:     end

[Validate]