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
-
.available? ⇒ Boolean
Checks if
jpegtran
is available. -
.optimize(path, options = { }, &block) ⇒ Struct
Performs optimizations above file.
Class Method Details
.available? ⇒ Boolean
Checks if jpegtran
is available.
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.
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, = { }, &block) # Command cmd = CommandBuilder::new(self::COMMAND) cmd.separators = ["-", " ", "-", " "] # Turn on/off arguments .each_pair do |k, v| if v.kind_of? TrueClass and self::BOOLEAN_ARGS.include? k cmd << k end end # Rotate if [:rotate].kind_of? Integer cmd.arg(:rotate, [:rotate].to_i) elsif .include? :rotate raise Exception::new("Invalid value for :rotate option. Integer expected.") end # Rotate if [:restart].kind_of? Integer cmd.arg(:restart, [:restart].to_i) elsif .include? :restart raise Exception::new("Invalid value for :restart option. Integer expected.") end # Crop if [:crop].kind_of? String cmd.arg(:crop, [:crop].to_s) elsif .include? :crop raise Exception::new("Invalid value for :crop option. Structured string expected. See 'jpegtran' reference.") end # Scans if [:scans].kind_of? String cmd.arg(:scans, [:scans].to_s) elsif .include? :scans raise Exception::new("Invalid value for :scans option. String expected.") end # Copy if .include? :copy value = [: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 .include? :flip value = [: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 .include? :outfile if [:outfile].kind_of? String value = [: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 [: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 |