Class: Kirigami::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/kirigami/image.rb

Overview

An Image file to be compressed and cut via Kirigami

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, max_size) ⇒ Image

Create a new Image

Parameters:

  • max_size

    An ImageSize to specify the size and name of image.



22
23
24
25
# File 'lib/kirigami/image.rb', line 22

def initialize(path, max_size)
  @path          = path
  @max_size      = max_size
end

Instance Attribute Details

#max_sizeObject (readonly)

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

Returns:

  • String



10
11
12
# File 'lib/kirigami/image.rb', line 10

def max_size
  @max_size
end

#pathObject (readonly)

The filepath of the image to be cut

Returns:

  • Pathname



17
18
19
# File 'lib/kirigami/image.rb', line 17

def path
  @path
end

Instance Method Details

#backup_filenameObject (private)

The filename for the backup File. Uses #filename + ".BAK"

Returns:

  • String



69
70
71
# File 'lib/kirigami/image.rb', line 69

def backup_filename
  File.basename(path) + ".BAK"
end

#backup_filepathObject (private)

The absolute filepath for the backup File

Returns:

  • Pathname



76
77
78
# File 'lib/kirigami/image.rb', line 76

def backup_filepath
  File.join(File.dirname(path), backup_filename)
end

#create_backup_copyObject (private)

Create a backup copy of the File first



48
49
50
# File 'lib/kirigami/image.rb', line 48

def create_backup_copy
  FileUtils.cp(target_filepath, backup_filepath) if Kirigami.config.safe_mode
end

#cut!Object

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

#filenameObject (private)

The filename for the target File

Returns:

  • String



62
63
64
# File 'lib/kirigami/image.rb', line 62

def filename
  File.basename(path)
end

#jpeg?Boolean (private)

Is the target File a JPEG?

Returns:

  • (Boolean)

    Boolean



83
84
85
# File 'lib/kirigami/image.rb', line 83

def jpeg?
  filename.ends_with?(".jpg")
end

#target_filepathObject (private)

The absolute filepath for the target File

Returns:

  • Pathname



55
56
57
# File 'lib/kirigami/image.rb', line 55

def target_filepath
  File.join(File.dirname(path), filename)
end