class ImageParadise::Optimizer
Constants
- CMD_TO_RUN
#¶ ↑
CMD_TO_RUN
¶ ↑#¶ ↑
- FILE
#¶ ↑
FILE
¶ ↑#¶ ↑
- NAMESPACE
#¶ ↑
NAMESPACE
¶ ↑#¶ ↑
Public Class Methods
[](i)
click to toggle source
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_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
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?
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
opnn()
click to toggle source
output?()
click to toggle source
rds(i)
click to toggle source
red?()
click to toggle source
rename_new_file_to_old_file(i = file?)
click to toggle source
report_stats(i = file?)
click to toggle source
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
run()
click to toggle source
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_old_filesize(i = file?)
click to toggle source
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
sfile(i = '')
click to toggle source
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
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
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