Class: Paperclip::Mozjpeg
- Inherits:
-
Processor
- Object
- Processor
- Paperclip::Mozjpeg
- Defined in:
- lib/paperclip-mozjpeg.rb
Overview
Handles JPEG and PNG compression.
Instance Attribute Summary collapse
-
#mozjpeg_options ⇒ Object
Returns the value of attribute mozjpeg_options.
-
#whiny ⇒ Object
Returns the value of attribute whiny.
Instance Method Summary collapse
- #cjpeg(arguments = "", local_options = {}) ⇒ Object
-
#initialize(file, options = {}, attachment = nil) ⇒ Mozjpeg
constructor
Compresses the
file
using MozJPEG’s cjpeg command. -
#make ⇒ Object
Performs the compression of the
file
.
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, = {}, = nil) super @geometry = [:geometry].to_s @mozjpeg_options = [:mozjpeg_options] @whiny = .fetch(:whiny, true) @use_system_binaries = .fetch(:use_system_binaries, false) @mozjpeg_path = .fetch(:mozjpeg_path, nil) @current_format = File.extname(@file.path) @basename = File.basename(@file.path, @current_format) end |
Instance Attribute Details
#mozjpeg_options ⇒ Object
Returns the value of attribute mozjpeg_options.
9 10 11 |
# File 'lib/paperclip-mozjpeg.rb', line 9 def @mozjpeg_options end |
#whiny ⇒ Object
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 = "", = {}) if !@use_system_binaries && defined?(::Mozjpeg::VERSION) cmd = ::Mozjpeg.cjpeg_path end cmd ||= ( @mozjpeg_path || 'cjpeg') Paperclip.run(cmd, arguments, ) end |
#make ⇒ Object
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 << parameters << "-outfile :dest" parameters << ":source" parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") success = cjpeg(parameters, :source => "#{File.(src.path)}", :dest => File.(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 |