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

new( commandline_arguments = ARGV, run_already = true ) { || ... } click to toggle source
#

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

crop( input_filename = input_filename?, use_these_crop_parameters = crop_parameters?, output_file = output_file? )
crop_parameters?() click to toggle source
#

crop_parameters?

#
# File lib/image_paradise/crop/crop.rb, line 199
def crop_parameters?
  @use_these_crop_parameters
end
do_crop( input_filename = input_filename?, use_these_crop_parameters = crop_parameters?, output_file = output_file? )
input?()
Alias for: input_filename?
input_file=(i = nil)
Alias for: set_input_file
input_file?() click to toggle source
#

input_file?

#
# File lib/image_paradise/crop/crop.rb, line 164
def input_file?
  @input_file
end
input_filename?() click to toggle source
#

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
Also aliased as: input?
menu( i = commandline_arguments? ) click to toggle source
#

menu

#
output?()
Alias for: output_file?
output_file=( i = :default )
Alias for: set_output_file
output_file?() click to toggle source
#

output_file?

This method will return the output-image file, which is the one that will be generated on a successful run.

#
# File lib/image_paradise/crop/crop.rb, line 133
def output_file?
  @output_file
end
Also aliased as: result, result?, output?
perform_crop_action( input_filename = input_filename?, use_these_crop_parameters = crop_parameters?, output_file = output_file? )
perform_the_crop_action( input_filename = input_filename?, use_these_crop_parameters = crop_parameters?, output_file = output_file? )
report() click to toggle source
#

report

#
# File lib/image_paradise/crop/crop.rb, line 361
def report
  report_where_the_file_is_now_stored
end
report_where_the_file_is_now_stored( be_verbose = be_verbose? ) click to toggle source
#

report_where_the_file_is_now_stored

#
# File lib/image_paradise/crop/crop.rb, line 319
def report_where_the_file_is_now_stored(
    be_verbose = be_verbose?
  )
  if be_verbose
    opn; e 'The output file should now be available at:'
    e
    e "  #{sfile(output_file?)}"
    e
  end
end
reset() click to toggle source
#

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
result()
Alias for: output_file?
result?()
Alias for: output_file?
return_the_default_name_for_cropped_image( i = input_file? ) click to toggle source
#

return_the_default_name_for_cropped_image

#
# File lib/image_paradise/crop/crop.rb, line 237
def return_the_default_name_for_cropped_image(
    i = input_file?
  )
  temp_directory?+
  'CROPPED_IMAGE_'+
  today+
  '_'+
  File.basename(i)
end
run() click to toggle source
#

run (run tag)

#
# File lib/image_paradise/crop/crop.rb, line 351
def run
  set_use_these_crop_parameters
  menu
  run_the_system_command
  report
end
run_the_system_command( input_filename = input_filename?, use_these_crop_parameters = crop_parameters?, output_file = output_file? ) click to toggle source
#

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_crop_command( i = :default )
set_crop_parameters( i = :default )
set_input_file(i = nil) click to toggle source
#

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
Also aliased as: input_file=
set_output_file( i = :default ) click to toggle source
#

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
Also aliased as: output_file=
set_store_here(i)
Alias for: set_temp_directory
set_temp_directory(i) click to toggle source
#

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
Also aliased as: set_store_here
set_use_these_crop_parameters( i = :default ) click to toggle source
#

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
synchronize_output_filename() click to toggle source
#

synchronize_output_filename

#
# File lib/image_paradise/crop/crop.rb, line 344
def synchronize_output_filename
  set_output_file :synchronize
end
temp_dir?()
Alias for: temp_directory?
temp_directory?() click to toggle source
#

temp_directory?

#
# File lib/image_paradise/crop/crop.rb, line 230
def temp_directory?
  @temp_directory
end
Also aliased as: temp_dir?