Module: Miniatura

Defined in:
lib/miniatura.rb,
lib/miniatura/logger.rb,
lib/miniatura/options.rb,
lib/miniatura/version.rb,
lib/miniatura/generate_command.rb

Defined Under Namespace

Classes: GenerateCommand, Logger, Options

Constant Summary collapse

VERSION =
"0.3.0"

Instance Method Summary collapse

Instance Method Details

#generate_thumb(options = {}) ⇒ Object

Raises:

  • (Errno::ENOENT)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/miniatura.rb', line 8

def generate_thumb options = {}
  options[:file_extension] ||= 'jpeg'
  options[:logger] = Rails.logger
  size = options[:size]
  video = MiniExiftool.new(current_path)
  orientation = video.rotation
  case orientation
  when 0
    options[:rotate] = 0
  when 90
    options[:rotate] = 90
  when 180
    options[:rotate] = 180
  when 270
    options[:rotate] = 270
  end
  video_width, video_height = video.imagewidth, video.imageheight
  case orientation
  when 0,180
    image_width = size
    ratio = size.to_f/video_width
    image_height = video_height * ratio
  else
    image_width = size
    ratio = size.to_f/video_height
    image_height = video_width * ratio
  end
  options[:size] = "#{image_width.to_i}" + "x" + "#{image_height.to_i}"
  tmp_path = File.join( File.dirname(current_path), "tmpfile.#{options[:file_extension]}")
  thumbnail = GenerateCommand.new(current_path, tmp_path)
  cmd = thumbnail.generate_command(options)
  logger = Miniatura::Logger.new(options).logger
  logger.info("Running command: #{cmd}")
  exit_code, error = nil
  raise Errno::ENOENT unless File.exist?(current_path)
  Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
    error =  stderr.read
    exit_code = wait_thr.value
  end
  handle_exit_code(exit_code, error, logger)
  File.rename tmp_path, current_path
end