Class: Paperclip::Document::Processors::Sketcher

Inherits:
Paperclip::Document::Processor show all
Defined in:
lib/paperclip/document/processors/sketcher.rb

Overview

This processor extract first page as thumbnail

Instance Attribute Summary collapse

Attributes inherited from Paperclip::Document::Processor

#instance, #tmp_dir

Instance Method Summary collapse

Methods inherited from Paperclip::Document::Processor

#basename, #file_path

Constructor Details

#initialize(file, options = {}, attachment = nil) ⇒ Sketcher

Returns a new instance of Sketcher.



8
9
10
11
12
13
14
15
16
17
# File 'lib/paperclip/document/processors/sketcher.rb', line 8

def initialize(file, options = {}, attachment = nil)
  super(file, options, attachment)
  @format = (options[:format] || :jpg).to_sym
  unless %i[jpg png].include?(@format)
    raise Paperclip::Error, 'Valid format must be specified'
  end
  unless @size = options[:size]
    @density = (options[:density] || 150).to_f
  end
end

Instance Attribute Details

#densityObject

Returns the value of attribute density.



6
7
8
# File 'lib/paperclip/document/processors/sketcher.rb', line 6

def density
  @density
end

#formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/paperclip/document/processors/sketcher.rb', line 6

def format
  @format
end

Instance Method Details

#makeObject

Extract the page



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/paperclip/document/processors/sketcher.rb', line 20

def make
  destination_path = tmp_dir.to_s
  options = { output: destination_path, pages: [1], format: [@format] }
  if @size
    options[:size] = @size
  elsif @density
    options[:density] = @density
  end
  begin
    Docsplit.extract_images(file_path.to_s, options)
  rescue
    raise Paperclip::Error, "There was an error extracting the first thumbnail from #{basename}"
  end
  File.open(File.join(destination_path, basename + "_1.#{@format}"))
end