class ImageParadise::Optimizer

Constants

CMD_TO_RUN
#

CMD_TO_RUN

#
FILE
#

FILE

#
NAMESPACE
#

NAMESPACE

#

Public Class Methods

[](i) click to toggle source
#

ImageParadise::Optimizer[]

#
# File lib/image_paradise/optimizer.rb, line 478
def self.[](i)
  new(i)
end
new( i = ARGV, run_already = true ) { || ... } click to toggle source
#

initialize

#
# File lib/image_paradise/optimizer.rb, line 42
def initialize(
    i           = ARGV,
    run_already = true
  )
  reset
  # ======================================================================= #
  # Handle Hash as input next. These is currently only used to disable
  # colours.
  # ======================================================================= #
  if block_given?
    yielded = yield
    if yielded.is_a? Hash
      # =================================================================== #
      # === :use_colours
      # =================================================================== #
      if yielded.has_key? :use_colours
        @use_colours = yielded.delete(:use_colours)
      end
      # =================================================================== #
      # === :may_we_exit
      # =================================================================== #
      if yielded.has_key? :may_we_exit
        set_may_we_exit(yielded.delete(:may_we_exit))
      end
    else
      case yielded
      when :do_not_exit
        @may_we_exit = false
      end
    end
  end
  set_these_files(i)
  run if run_already
end

Public Instance Methods

check_whether_the_command_is_available_or_not() click to toggle source
#

check_whether_the_command_is_available_or_not

We check whether the commands are available or not.

#
# File lib/image_paradise/optimizer.rb, line 436
def check_whether_the_command_is_available_or_not
  result = `#{cmd?} 2>&1`
  is_available = true
  is_available = false if result.include? cmd?+': not found'
  if is_available == false
    opnn
    e 'The program `'+sfancy(cmd?)+'` is '+red?+'not'+rev+
      ' available. Please install it first.'
    if is_on_roebe?
      e 'As this is a roebe-system, we will try to make use of'
      e 'the RBT project in order to compile this program next.'
      try_to_compile_this_program(cmd?)
    end
    exit
  end
end
cmd?()
Alias for: cmd_to_run?
cmd_to_run?() click to toggle source
#

cmd_to_run?

#
# File lib/image_paradise/optimizer.rb, line 135
def cmd_to_run?
  @cmd_to_run
end
Also aliased as: cmd?
consider_reporting_how_many_image_files_we_did_process() click to toggle source
#

consider_reporting_how_many_image_files_we_did_process

#
# File lib/image_paradise/optimizer.rb, line 387
def consider_reporting_how_many_image_files_we_did_process
  if @processed_n_files > 1 # Only if we have at least processed 2 images.
    opnn
    e 'Finished! We processed '+sfancy(@processed_n_files.to_s)+' '+
      @obtain_this_image_format+' files.'
  end
end
determine_output_file(i) click to toggle source
#

determine_output_file

This will determine the instance variable @output_file.

#
# File lib/image_paradise/optimizer.rb, line 323
def determine_output_file(i)
  @output_file = i.dup # Will store here.
  @output_file[@output_file.index('.'),0] = '-fs8' # Append "-fs8".
end
files?()
Alias for: these_files?
image_format?()
input?()
Alias for: these_files?
is_jpeg_file?(i)
Alias for: is_png_file?
is_png_file?(i) click to toggle source
#

is_png_file?

#
# File lib/image_paradise/optimizer.rb, line 296
def is_png_file?(i) # check whether this file is a .png file or not.
  return true if File.extname(i) == image_format?
  false
end
Also aliased as: is_jpeg_file?
menu(i) click to toggle source
#

menu

#
obtain_this_image_format?() click to toggle source
#

obtain_this_image_format?

#
# File lib/image_paradise/optimizer.rb, line 289
def obtain_this_image_format?
  @obtain_this_image_format
end
Also aliased as: image_format?
open_this_file_here_then_exit() click to toggle source
#

open_this_file_here_then_exit

#
# File lib/image_paradise/optimizer.rb, line 208
def open_this_file_here_then_exit
  _ = 'bluefish '+FILE
  opnn; e _; system _
  exit
end
opnn() click to toggle source
#

opnn

#
# File lib/image_paradise/optimizer.rb, line 398
def opnn
  Opn.opn(
    namespace: NAMESPACE, use_colours: @use_colours
  )
end
output?() click to toggle source
#

output?

#
# File lib/image_paradise/optimizer.rb, line 373
def output?
  @output_file
end
rds(i) click to toggle source
#

rds

#
# File lib/image_paradise/optimizer.rb, line 128
def rds(i)
  i.squeeze('/')
end
red?() click to toggle source
#

red?

#
# File lib/image_paradise/optimizer.rb, line 380
def red?
  Colours::RED
end
rename_new_file_to_old_file(i = file?) click to toggle source
#

rename_new_file_to_old_file

#
# File lib/image_paradise/optimizer.rb, line 314
def rename_new_file_to_old_file(i = file?)
  FileUtils.mv(output?, i)
end
report_stats(i = file?) click to toggle source
#

report_stats

#
# File lib/image_paradise/optimizer.rb, line 270
def report_stats(i = file?)
  opnn
  e 'The old filesize of `'+sfile(i)+'` was: '+
     sfancy('%6s' % @old_filesize.to_s)
  opnn
  e 'The new filesize of `'+sfile(i)+'` is:  '+
     sfancy('%6s' % @new_filesize.to_s)
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method ImageParadise::Base#reset
# File lib/image_paradise/optimizer.rb, line 80
def reset
  super()
  # ======================================================================= #
  # === @may_we_exit
  # ======================================================================= #
  @may_we_exit = true
  # ======================================================================= #
  # === @output_file
  # ======================================================================= #
  @output_file = nil
  # ======================================================================= #
  # === @processed_n_files
  #
  # Keep track of how many image-files we already processed.
  # ======================================================================= #
  @processed_n_files = 0
  # ======================================================================= #
  # === @cmd_to_run
  # ======================================================================= #
  @cmd_to_run = CMD_TO_RUN
  # ======================================================================= #
  # === @use_colours
  # ======================================================================= #
  @use_colours = true
  # ======================================================================= #
  # === @obtain_this_image_format
  #
  # We can work either on .png files or on .jpg files.
  #
  # The following ivar will keep track of that.
  # ======================================================================= #
  @obtain_this_image_format = '.png'
end
rev() click to toggle source
#

rev

#
# File lib/image_paradise/optimizer.rb, line 247
def rev
  Colours.rev
end
run() click to toggle source
#

run

#
# File lib/image_paradise/optimizer.rb, line 470
def run
  check_whether_the_command_is_available_or_not
  work_on_the_images
end
run_sys_command(i) click to toggle source
#

run_sys_command

The first argument should be an existing, local file such as “foobar.jpg”, without the quotes.

#
# File lib/image_paradise/optimizer.rb, line 410
def run_sys_command(i)
  if i.include? "'"
    i = '"'+i+'"'
  end
  _ = cmd?.to_s.dup
  _ << ' --force' if i.include? 'png'
  _ << ' --quality=40-100 --strip --skip-if-larger --verbose'
  _ << " #{i}" # This is the filename here.
  opnn
  esystem(_)
  if File.exist? output? # Check whether the output file exists.
    @new_filesize = File.size(output?)
  else
    opnn; e "The file `#{sfile(output?)}` does not exist."
    if @may_we_exit
      opnn; e 'Thus exiting now.'
      exit
    end
  end
end
sanitize_these_files() click to toggle source
#

sanitize_these_files

#
# File lib/image_paradise/optimizer.rb, line 171
def sanitize_these_files
  @these_files.map! {|entry|
    entry = entry.to_s.dup
    entry.strip!
    case entry # Next, act on a few special instructions. (case tag)
    # ===================================================================== #
    # === ALL_IMAGES
    # ===================================================================== #
    when 'ALL_IMAGES','ALL_PNG','ALLPNG','ALL_PNG_IMAGES','ALL',
         nil
      entry = Dir['*'+image_format?]
    # ===================================================================== #
    # === DEEP
    # ===================================================================== #
    when 'DEEP','ALL_SUBDIRS' # As above but includes all subdirectories.
      entry = Dir['**/*'+image_format?]
    end
    entry
  }
  @these_files.flatten!
end
set_may_we_exit(i) click to toggle source
#

set_may_we_exit

#
# File lib/image_paradise/optimizer.rb, line 117
def set_may_we_exit(i)
  case i
  when :do_not_exit
    i = false
  end
  @may_we_exit = i
end
set_old_filesize(i = file?) click to toggle source
#

set_old_filesize

#
# File lib/image_paradise/optimizer.rb, line 304
def set_old_filesize(i = file?)
  if File.exist? i
    @old_filesize = File.size(i)
    determine_output_file(i)
  end
end
set_these_files(i = 'ALL_PNG_IMAGES') click to toggle source
#

set_these_files

We always have to work on an Array.

#
# File lib/image_paradise/optimizer.rb, line 144
def set_these_files(i = 'ALL_PNG_IMAGES')
  if i.is_a? String
    i = i.to_s.dup
    i.strip!
    # ===================================================================== #
    # Assume that the user did input a String with internal ' '.
    # This means that we will split up this String and assume
    # that the entries will be valid files.
    # ===================================================================== #
    i = i.split(' ') if i.include? ' '
  end
  i = [i] unless i.is_a? Array # After this point, we have an array.
  menu(i)
  if i.any? {|_| _.include? 'localhost' }
    i.map! {|entry|
      _ = ENV['MY_DATA']+'/'
      entry = entry.gsub(/localhost/, _).
                    gsub(/^http:\/\//,'')
    }
  end
  @these_files = i
  sanitize_these_files # Always optimize it after the assignment.
end
sfancy(i = '') click to toggle source
#

sfancy

#
# File lib/image_paradise/optimizer.rb, line 254
def sfancy(i = '')
  return Colours.sfancy(i) if use_colours?
  return i
end
sfile(i = '') click to toggle source
#

sfile

#
# File lib/image_paradise/optimizer.rb, line 262
def sfile(i = '')
  return Colours.sfile(i)if use_colours?
  return i
end
show_help_then_exit() click to toggle source
#

show_help_then_exit (help tag)

#
# File lib/image_paradise/optimizer.rb, line 196
def show_help_then_exit
  e
  Colours.ecomment '  ALL_IMAGES # Work on all images but only in the current directory.'
  Colours.ecomment '  DEEP       # Work on all images, including subdirectories.'
  Colours.ecomment '  OPEN       # Open this file here.'
  e
  exit
end
these_files?() click to toggle source
#

these_files?

#
# File lib/image_paradise/optimizer.rb, line 239
def these_files?
  @these_files
end
Also aliased as: files?, input?
try_to_compile_this_program(i = cmd?) click to toggle source
#

try_to_compile_this_program

This method will tab into the RBT project and compile the given input at hand - which is typically “pngquant”.

The method was added mostly as a convenience-method, to quickly install pngquant.

#
# File lib/image_paradise/optimizer.rb, line 462
def try_to_compile_this_program(i = cmd?)
  require 'rbt'
  RBT::Compile.new(i)
end
use_colours?() click to toggle source
#

use_colours?

#
# File lib/image_paradise/optimizer.rb, line 282
def use_colours?
  @use_colours
end
work_on_the_images() click to toggle source
#

work_on_the_images

#
# File lib/image_paradise/optimizer.rb, line 331
def work_on_the_images
  files?.each {|the_file|
    the_file = rds(the_file)
    extname = File.extname(the_file).delete('.')
    case extname
    # ===================================================================== #
    # === Handle .jpg or .jpeg files here
    # ===================================================================== #
    when 'jpg','jpeg'
      @processed_n_files += 1 # Increment the counter very early on.
      @cmd_to_run = 'jpegoptim'
      esystem @cmd_to_run+' '+the_file
    # ===================================================================== #
    # === Handle .png files here
    # ===================================================================== #
    when 'png'
      if File.exist? the_file
        @processed_n_files += 1 # Increment the counter very early on.
        if is_png_file?(the_file) # main tag
          set_old_filesize(the_file)
          run_sys_command(the_file)
          rename_new_file_to_old_file(the_file) # <-- Important for cleanup.
          report_stats(the_file)
        else
          opnn
          e 'The file '+sfile(the_file)+' is '+RED+'not'+rev+
            ' a '+image_format?+' file. We can not optimize it.'
        end
      else
        opnn
        e 'No file could be found at `'+sfile(the_file)+'`.'
      end
    else
      e 'Unsure what to do with `'+sfile(the_file)+'`.'
    end
  }
  consider_reporting_how_many_image_files_we_did_process
end