Class: PictureTag::SourceImage

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_picture_tag/images/source_image.rb

Overview

Handles a given source image file and its properties. Provides a speed advantage by storing expensive file reads and writes in instance variables, to be reused by many different generated images.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relative_filename, media_preset = nil) ⇒ SourceImage

include MiniMagick



10
11
12
13
14
15
16
17
18
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 10

def initialize(relative_filename, media_preset = nil)
  # /home/dave/my_blog/assets/images/somefolder/myimage.jpg
  #                                  ^^^^^^^^^^^^^^^^^^^^^^
  @shortname = relative_filename
  @media_preset = media_preset

  @missing = missing?
  check_cache
end

Instance Attribute Details

#media_presetObject (readonly)

Returns the value of attribute media_preset.



6
7
8
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 6

def media_preset
  @media_preset
end

#missingObject (readonly)

Returns the value of attribute missing.



6
7
8
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 6

def missing
  @missing
end

#shortnameObject (readonly)

Returns the value of attribute shortname.



6
7
8
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 6

def shortname
  @shortname
end

Instance Method Details

#base_nameObject

/home/dave/my_blog/assets/images/somefolder/myimage.jpg ^^^^^^^^^^^^^^^^^^



66
67
68
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 66

def base_name
  @shortname.delete_suffix File.extname(@shortname)
end

#cropObject



24
25
26
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 24

def crop
  PictureTag.crop(media_preset)
end

#crop?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 28

def crop?
  !crop.nil?
end

#cropped_aspectObject



52
53
54
55
56
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 52

def cropped_aspect
  return Utils.aspect_float(raw_width, raw_height) unless crop?

  Utils.aspect_float(*crop.split(':').map(&:to_f))
end

#digestObject



20
21
22
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 20

def digest
  @digest ||= cache[:digest] || ''
end

#dimensionsObject



36
37
38
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 36

def dimensions
  [width, height]
end

#extObject

/home/dave/my_blog/assets/images/somefolder/myimage.jpg ^^^



72
73
74
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 72

def ext
  @ext ||= File.extname(name)[1..].downcase
end

#heightObject



46
47
48
49
50
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 46

def height
  return raw_height unless crop?

  [raw_height, (raw_width / cropped_aspect)].min.round
end

#keepObject



32
33
34
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 32

def keep
  PictureTag.keep(media_preset)
end

#nameObject

/home/dave/my_blog/assets/images/somefolder/myimage.jpg ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



60
61
62
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 60

def name
  @name ||= File.join(PictureTag.source_dir, @shortname)
end

#widthObject



40
41
42
43
44
# File 'lib/jekyll_picture_tag/images/source_image.rb', line 40

def width
  return raw_width unless crop?

  [raw_width, (raw_height * cropped_aspect)].min.round
end