class Kirigami::Image

An Image file to be compressed and cut via Kirigami

Attributes

max_size[R]

An ImageMagick Image Geometry for the target file size www.imagemagick.org/script/command-line-processing.php#geometry

Returns String

path[R]

The filepath of the image to be cut

Returns Pathname

Public Class Methods

new(path, max_size) click to toggle source

Create a new Image

max_size - An ImageSize to specify the size and name of image.

# File lib/kirigami/image.rb, line 22
def initialize(path, max_size)
  @path          = path
  @max_size      = max_size
end

Public Instance Methods

cut!() click to toggle source

Cuts the File down to size! Creates a backup copy first, if required.

# File lib/kirigami/image.rb, line 28
def cut!
  create_backup_copy
  MiniMagick::Tool::Mogrify.new do |mogrify|
    mogrify.resize(max_size)
    mogrify.strip
    if jpeg?
      mogrify.colorspace(Kirigami.config.jpeg_colorspace)
      mogrify.sampling_factor(Kirigami.config.jpeg_sampling_factor)
      mogrify.interlace(Kirigami.config.jpeg_interlacing)
      mogrify.quality(Kirigami.config.jpeg_compression_quality)
    end
    mogrify << target_filepath
  end
end

Private Instance Methods

backup_filename() click to toggle source

The filename for the backup File. Uses {filename} + “.BAK”

Returns String

# File lib/kirigami/image.rb, line 69
def backup_filename
  File.basename(path) + ".BAK"
end
backup_filepath() click to toggle source

The absolute filepath for the backup File

Returns Pathname

# File lib/kirigami/image.rb, line 76
def backup_filepath
  File.join(File.dirname(path), backup_filename)
end
create_backup_copy() click to toggle source

Create a backup copy of the File first

# File lib/kirigami/image.rb, line 48
def create_backup_copy
  FileUtils.cp(target_filepath, backup_filepath) if Kirigami.config.safe_mode
end
filename() click to toggle source

The filename for the target File

Returns String

# File lib/kirigami/image.rb, line 62
def filename
  File.basename(path)
end
jpeg?() click to toggle source

Is the target File a JPEG?

Returns Boolean

# File lib/kirigami/image.rb, line 83
def jpeg?
  filename.ends_with?(".jpg")
end
target_filepath() click to toggle source

The absolute filepath for the target File

Returns Pathname

# File lib/kirigami/image.rb, line 55
def target_filepath
  File.join(File.dirname(path), filename)
end