Class: Imdhemy::Jekyll::Theme::SipsJpegOptimizer

Inherits:
Object
  • Object
show all
Defined in:
lib/imdhemy/jekyll/theme/image_cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSipsJpegOptimizer

Returns a new instance of SipsJpegOptimizer.



251
252
253
# File 'lib/imdhemy/jekyll/theme/image_cli.rb', line 251

def initialize
  @label = "sips"
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



249
250
251
# File 'lib/imdhemy/jekyll/theme/image_cli.rb', line 249

def label
  @label
end

Instance Method Details

#optimize(path) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/imdhemy/jekyll/theme/image_cli.rb', line 255

def optimize(path)
  Dir.mktmpdir("imdhemy-image") do |tmpdir|
    temp_output = Pathname(tmpdir).join(path.basename.to_s)
    success = system(
      "sips",
      "-s", "format", "jpeg",
      "-s", "formatOptions", "best",
      path.to_s,
      "--out", temp_output.to_s,
      out: File::NULL,
      err: File::NULL
    )
    raise "Optimizer failed: sips" unless success

    if !temp_output.exist? || temp_output.size >= path.size
      return { changed: false, message: "#{label} ran but produced no smaller output" }
    end

    FileUtils.cp(temp_output, path)
    { changed: true, message: label }
  end
end