Class: Paperclip::Mozjpeg

Inherits:
Processor
  • Object
show all
Defined in:
lib/paperclip-mozjpeg.rb

Overview

Handles JPEG and PNG compression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Compresses the file using MozJPEG’s cjpeg command. Compression will raise no errors unless whiny is true (which it is, by default).

Options include:

+geometry+ - the desired width and height of the thumbnail (required)
+whiny+ - whether to raise an error when processing fails. Defaults to true


19
20
21
22
23
24
25
26
27
28
# File 'lib/paperclip-mozjpeg.rb', line 19

def initialize(file, options = {}, attachment = nil)
  super
  @geometry             = options[:geometry].to_s
  @mozjpeg_options      = options[:mozjpeg_options]
  @whiny                = options.fetch(:whiny, true)
  @use_system_binaries  = options.fetch(:use_system_binaries, false)
  @mozjpeg_path         = options.fetch(:mozjpeg_path, nil)
  @current_format       = File.extname(@file.path)
  @basename             = File.basename(@file.path, @current_format)
end

Instance Attribute Details

#mozjpeg_optionsObject

Returns the value of attribute mozjpeg_options.



9
10
11
# File 'lib/paperclip-mozjpeg.rb', line 9

def mozjpeg_options
  @mozjpeg_options
end

#whinyObject

Returns the value of attribute whiny.



9
10
11
# File 'lib/paperclip-mozjpeg.rb', line 9

def whiny
  @whiny
end

Instance Method Details

#cjpeg(arguments = "", local_options = {}) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/paperclip-mozjpeg.rb', line 56

def cjpeg(arguments = "", local_options = {})
  if !@use_system_binaries && defined?(::Mozjpeg::VERSION)
    cmd = ::Mozjpeg.cjpeg_path
  end
  cmd ||= ( @mozjpeg_path || 'cjpeg')
  Paperclip.run(cmd, arguments, local_options)
end

#makeObject

Performs the compression of the file. Returns the Tempfile that contains the new image.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/paperclip-mozjpeg.rb', line 32

def make
  src = @file
  filename = [@basename, @format ? ".#{@format}" : ""].join
  dst = TempfileFactory.new.generate(filename)

  begin
    parameters = []
    parameters << mozjpeg_options
    parameters << "-outfile :dest"
    parameters << ":source"

    parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

    success = cjpeg(parameters, :source => "#{File.expand_path(src.path)}", :dest => File.expand_path(dst.path))
  # dynamic exception handling to support paperclip using Cocaine or Terrapin gem
  rescue Class.new { def self.===(ex); ex.class.name.end_with?("::ExitStatusError"); end }
    raise Paperclip::Error, "There was an error processing the file for #{@basename}" if @whiny
  rescue Class.new { def self.===(ex); ex.class.name.end_with?("::CommandNotFoundError"); end }
    raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `cjpeg` command. Please install MozJPEG.")
  end

  dst
end