Module: Redmine::Thumbnail

Extended by:
Utils::Shell
Defined in:
lib/redmine/thumbnail.rb

Overview

Since:

  • 2.1.0

Constant Summary collapse

CONVERT_BIN =
(Redmine::Configuration['imagemagick_convert_command'] || 'convert').freeze
ALLOWED_TYPES =
%w(image/bmp image/gif image/jpeg image/png)

Class Method Summary collapse

Methods included from Utils::Shell

shell_quote, shell_quote_command

Class Method Details

.convert_available?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'lib/redmine/thumbnail.rb', line 50

def self.convert_available?
  return @convert_available if defined?(@convert_available)
  @convert_available = system("#{shell_quote CONVERT_BIN} -version") rescue false
  logger.warn("Imagemagick's convert binary (#{CONVERT_BIN}) not available") unless @convert_available
  @convert_available
end

.generate(source, target, size) ⇒ Object

Generates a thumbnail for the source image to target



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redmine/thumbnail.rb', line 29

def self.generate(source, target, size)
  return nil unless convert_available?
  unless File.exists?(target)
    # Make sure we only invoke Imagemagick if the file type is allowed
    unless File.open(source) {|f| ALLOWED_TYPES.include? MimeMagic.by_magic(f).try(:type) }
      return nil
    end
    directory = File.dirname(target)
    unless File.exists?(directory)
      FileUtils.mkdir_p directory
    end
    size_option = "#{size}x#{size}>"
    cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source} -auto-orient -thumbnail #{shell_quote size_option} #{shell_quote target}"
    unless system(cmd)
      logger.error("Creating thumbnail failed (#{$?}):\nCommand: #{cmd}")
      return nil
    end
  end
  target
end

.loggerObject



57
58
59
# File 'lib/redmine/thumbnail.rb', line 57

def self.logger
  Rails.logger
end