Module: Redmine::Thumbnail

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

Constant Summary collapse

CONVERT_BIN =
(Redmine::Configuration['imagemagick_convert_command'] || 'convert').freeze

Class Method Summary collapse

Methods included from Utils::Shell

shell_quote

Class Method Details

.convert_available?Boolean

Returns:

  • (Boolean)

44
45
46
47
48
49
# File 'lib/redmine/thumbnail.rb', line 44

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


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/redmine/thumbnail.rb', line 27

def self.generate(source, target, size)
  return nil unless convert_available?
  unless File.exists?(target)
    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} -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


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

def self.logger
  Rails.logger
end