Module: Jpegtran

Defined in:
lib/jpegtran.rb

Overview

The jpegtran tool command frontend.

Defined Under Namespace

Classes: Result

Constant Summary collapse

COMMAND =

Holds jpegoptim command.

:jpegtran
BOOLEAN_ARGS =

Indicates turn on/off style arguments.

Set::new [
    :optimize, :progressive, :grayscale, :perfect, :transpose, 
    :transverse, :trim, :arithmetic
]
COPY_OPTIONS =

Holds copy values.

Set::new [
    :none, :comments, :all
]
FLIP_OPTIONS =

Holds flip values.

Set::new [
    :horizontal, :vertical
]
ERROR =

Holds output matchers.

/jpegtran:\s*(.*)\s*/

Class Method Summary collapse

Class Method Details

.available?Boolean

Checks if jpegtran is available.

Returns:

  • (Boolean)

    true if it is, false in otherwise



67
68
69
# File 'lib/jpegtran.rb', line 67

def self.available?
    return Whereis.available? self::COMMAND 
end

.optimize(path, options = { }, &block) ⇒ Struct

Performs optimizations above file. For list of arguments, see reference of jpegtran.

If block is given, runs jpegoptim asynchronously. In that case, em-pipe-run file must be already required.

Parameters:

  • paths (String, Array)

    file path or array of paths for optimizing

  • options (Hash) (defaults to: { })

    options

  • block (Proc)

    block for giving back the results

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/jpegtran.rb', line 84

def self.optimize(path, options = { }, &block)

    # Command
    cmd = CommandBuilder::new(self::COMMAND)
    cmd.separators = ["-", " ", "-", " "]
    
    # Turn on/off arguments
    options.each_pair do |k, v|
        if v.kind_of? TrueClass and self::BOOLEAN_ARGS.include? k 
            cmd << k
        end
    end
    
    # Rotate
    if options[:rotate].kind_of? Integer
        cmd.arg(:rotate, options[:rotate].to_i)
    elsif options.include? :rotate
        raise Exception::new("Invalid value for :rotate option. Integer expected.")
    end
    
    # Rotate
    if options[:restart].kind_of? Integer
        cmd.arg(:restart, options[:restart].to_i)
    elsif options.include? :restart
        raise Exception::new("Invalid value for :restart option. Integer expected.")
    end
    
    # Crop
    if options[:crop].kind_of? String
        cmd.arg(:crop, options[:crop].to_s)
    elsif options.include? :crop
        raise Exception::new("Invalid value for :crop option. Structured string expected. See 'jpegtran' reference.")
    end
    
    # Scans
    if options[:scans].kind_of? String
        cmd.arg(:scans, options[:scans].to_s)
    elsif options.include? :scans
        raise Exception::new("Invalid value for :scans option. String expected.")
    end
            
    # Copy
    if options.include? :copy 
        value = options[:copy].to_sym
        if self::COPY_OPTIONS.include? value
            cmd.arg(:copy, value)
        else
            raise Exception::new("Invalid value for :copy. Expected " << self::COPY_OPTIONS.to_s)
        end
    end 
    
    # Flip
    if options.include? :flip
        value = options[:flip].to_sym
        if self::FLIP_OPTIONS.include? :value
            cmd.arg(:flip, value)
        else
            raise Exception::new("Invalid value for :flip. Expected " << self::FLIP_OPTIONS.to_s)
        end
    end
    
    # Outfile
    if options.include? :outfile
        if options[:outfile].kind_of? String
            value = options[:outfile].to_s
        else
            raise Exception::new("Invalid value for :outfile option. String expected.")
        end
    else
        value = path.to_s
    end
    
    cmd.arg(:outfile, value)
    
    # Runs the command
    cmd << path.to_s
    
    if options[:debug] == true
        STDERR.write cmd.to_s + "\n"
    end
        
    # Blocking
    if block.nil?
        cmd.execute!

    # Non-blocking
    else
        cmd.execute do |output|
            block.call()
        end
    end
    
end