class ImageParadise::Crop
Constants
- CONVERT_COMMAND
#¶ ↑
CONVERT_COMMAND
¶ ↑#¶ ↑
- CROP_PARAMETERS_TO_USE
#¶ ↑
CROP_PARAMETERS_TO_USE
¶ ↑These are the default parameters to be given to the crop-interface of convert.
#¶ ↑
- TEMP_DIRECTORY
#¶ ↑
TEMP_DIRECTORY
¶ ↑Here it is specified which directory is used for the creation of the output-image file. This can be overruled by calling
set_output_file
() with another argument.#¶ ↑
Public Class Methods
#¶ ↑
initialize¶ ↑
This method only accepts two arguments. The first is the commandline arguments.
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 46 def initialize( commandline_arguments = ARGV, run_already = true ) reset set_commandline_arguments( commandline_arguments ) # ======================================================================= # # === Handle blocks given to this method next # # This must come after reset() has been called already. # ======================================================================= # if block_given? yielded = yield case yielded # ===================================================================== # # === :do_not_run_yet_and_be_verbose # ===================================================================== # when :do_not_run_yet_and_be_verbose run_already = false @be_verbose = false # ===================================================================== # # === :do_not_run_yet # ===================================================================== # when :do_not_run_yet run_already = false else # =================================================================== # # === Handles Hashes next # =================================================================== # if yielded.is_a? Hash # ================================================================= # # === :store_in_this_directory # ================================================================= # if yielded.has_key? :store_in_this_directory set_store_here( yielded.delete(:store_in_this_directory) ) end end end end case run_already # ======================================================================= # # === :dont_run_yet # ======================================================================= # when :dont_run_yet, :do_not_run_yet run_already = false run_already = false end run if run_already end
Public Instance Methods
#¶ ↑
input_filename?¶ ↑
This is guaranteed to be a String
.
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 173 def input_filename? @input_file.to_s end
#¶ ↑
reset (reset tag)¶ ↑
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 104 def reset # ======================================================================= # # === @temp_dir # ======================================================================= # if File.directory? TEMP_DIRECTORY @temp_directory = TEMP_DIRECTORY else @temp_directory = (Dir.pwd+'/').squeeze '/' end # ======================================================================= # # === @be_verbose # ======================================================================= # @be_verbose = true # ======================================================================= # # === @output_file # # We will use some dummy-default name initially. This will become # more correct if the user supplied a proper filename as the main # file to modify. # ======================================================================= # @output_file = 'output.png' end
#¶ ↑
run_the_system_command
(system tag, sys tag, crop tag)¶ ↑
This method will perform the actual cropping-action, by delegation towards “convert”.
The first input-argument should be the filename of the file that is to be cropped, such as “foobar.png”.
The second input-argument should be the specific crop-parameters that we are wanting to apply.
A typical crop-argument in this regard looks like this:
-crop 40x30+40+30
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 264 def run_the_system_command( input_filename = input_filename?, use_these_crop_parameters = crop_parameters?, output_file = output_file? ) if input_filename.is_a? Array input_filename = input_filename.join(' ') end if use_these_crop_parameters.empty? use_these_crop_parameters = CROP_PARAMETERS_TO_USE end # ======================================================================= # # Next make sure that the parameters are a String. # ======================================================================= # use_these_crop_parameters = use_these_crop_parameters.to_s.dup # ======================================================================= # # The following code means that the user passed only one +5 rather # than two +5+5, hence we sanitize it here a little bit. # ======================================================================= # if use_these_crop_parameters.count('+') == 1 use_these_crop_parameters << use_these_crop_parameters[ use_these_crop_parameters.index('+')..-1 ] end if use_these_crop_parameters.include? '%' use_these_crop_parameters << 'x+0+0' unless use_these_crop_parameters.start_with?('x') end # ======================================================================= # # In the past we synchronized this back, but since as of March 2021 # we no longer do so. # ======================================================================= # # set_crop_parameters(use_this_crop_command) # Synchronize it back here. # ======================================================================= # if be_verbose? opn; e 'Next cropping the image `'+sfancy(input_filename)+'`.' end _ = CONVERT_COMMAND+' '+ input_filename+' -crop '+ use_these_crop_parameters+ ' '+ output_file if File.exist? input_filename opn; esystem _ else e 'No file exists at '+sfile(input_filename) end return output_file # It is also useful to return the file path of the generated file here. end
#¶ ↑
set_input_file
¶ ↑
Whenever this method is called, the output-filename will also be determined, via the method called synchronize_output_filename
().
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 155 def set_input_file(i = nil) i = i.first if i.is_a? Array @input_file = i synchronize_output_filename if i end
#¶ ↑
set_output_file
¶ ↑
Designate the output file via this method.
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 208 def set_output_file( i = :default ) i = i.first if i.is_a? Array case i when nil, :default, :synchronize, :infer i = return_the_default_name_for_cropped_image end if i unless i.include? '/' i = File.absolute_path(i) end end @output_file = i end
#¶ ↑
set_temp_directory
¶ ↑
This method will designate the temp-directory in use.
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 144 def set_temp_directory(i) i << '/' unless i.end_with? '/' @temp_directory = i end
#¶ ↑
set_use_these_crop_parameters
¶ ↑
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 180 def set_use_these_crop_parameters( i = :default ) i = i.join(' ').strip if i.is_a? Array case i # ======================================================================= # # This is the default entry point here. # ======================================================================= # when nil, :default i = CROP_PARAMETERS_TO_USE end @use_these_crop_parameters = i end