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

Handles thumbnailing images that are uploaded.

### Methods

convert_options? crop? make new transformation_command

### Attributes

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

### Public Class methods

new(file, options = {})

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 vendor/plugins/paperclip/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

### Public Instance methods

convert_options?()

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

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/thumbnail.rb, line 34
    34:     def convert_options?
    35:       not @convert_options.blank?
    36:     end

crop?()

Returns true if the `target_geometry` is meant to crop.

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/thumbnail.rb, line 29
    29:     def crop?
    30:       @crop
    31:     end

make()

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

[Source]

        # File vendor/plugins/paperclip/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

transformation_command()

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

[Source]

        # File vendor/plugins/paperclip/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

[Validate]