Class: Image

Inherits:
Zarchitect show all
Defined in:
lib/zarchitect/image.rb

Constant Summary collapse

@@search =
false

Constants inherited from Zarchitect

Zarchitect::ASSETDIR, Zarchitect::ASSETSDIR, Zarchitect::BUILDIR, Zarchitect::CONFIGDIR, Zarchitect::DEBUGSDIR, Zarchitect::DRAFTDIR, Zarchitect::FILEDIR, Zarchitect::FILESDIR, Zarchitect::HTMLDIR, Zarchitect::LAYOUTDIR, Zarchitect::NODEDIR, Zarchitect::SHARESDIR, Zarchitect::VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Zarchitect

#main, #rss

Constructor Details

#initialize(path, f) ⇒ Image

+++++++++++++++++++++++++++++



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zarchitect/image.rb', line 16

def initialize(path, f)
  @thumbf = f
  @path = path
  unless @thumbf
    @url  = path.clone
    @url[0] = "/" # replace _ with /
  else
    # thumbnail
    @url  = path[(HTMLDIR.length)..-1]
  end
  @dimensions = Point.new(0,0)
  #=============================== [0] = realpath
  # [1] = type
  # [2] = Dimensions
  #=============================== [3] = Dimensions+?+?
  #=============================== [4] = Color depth
  #=============================== [5] = Color space
  #=============================== [6] = Bytes // not accurate for ani-gif
  #=============================== [7] = ?
  #=============================== [8] = ?
  arr = %x{identify #{@path}}.split(" ")
  dim = arr[2].split("x")
  @dimensions.x = dim[0].to_i
  @dimensions.y = dim[1].to_i
  #@size         = arr[6].to_i
  @size         = File.size(path)
  @type         = arr[1].clone
  # validate file size
  if @size > Zarchitect.conf.image_limit.to_f.mib_to_bytes
    GPI.print "Error: File #{path} too large "\
      "(#{@size.bytes_to_mib.to_f.round(2)}MiB)."\
      " Allowed size: #{Zarchitect.conf.image_limit.to_f.mb_to_mib.round(2)}"
    GPI.quit
  end
  strip_exif unless @thumbf
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



2
3
4
# File 'lib/zarchitect/image.rb', line 2

def dimensions
  @dimensions
end

#sizeObject (readonly)

Returns the value of attribute size.



2
3
4
# File 'lib/zarchitect/image.rb', line 2

def size
  @size
end

#thumbl_f=(value) ⇒ Object (writeonly)

Sets the attribute thumbl_f

Parameters:

  • value

    the value to set the attribute thumbl_f to.



3
4
5
# File 'lib/zarchitect/image.rb', line 3

def thumbl_f=(value)
  @thumbl_f = value
end

#thumbs_f=(value) ⇒ Object (writeonly)

Sets the attribute thumbs_f

Parameters:

  • value

    the value to set the attribute thumbs_f to.



3
4
5
# File 'lib/zarchitect/image.rb', line 3

def thumbs_f=(value)
  @thumbs_f = value
end

#typeObject (readonly)

Returns the value of attribute type.



2
3
4
# File 'lib/zarchitect/image.rb', line 2

def type
  @type
end

Class Method Details

.find(k, v) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/zarchitect/image.rb', line 124

def self.find(k, v)
  @@search = true
  # o = Image.find("url", "/files/projects/tre/screen1.png") // usage
  GPI.print "Looking for img: #{v}", GPI::CLU.check_option('v')
  ObjectSpace.each_object(ImageSet) do |set|
    str = set.orig.send(k)
    #GPI.print "v:   #{v}", GPI::CLU.check_option('v')
    #GPI.print "str: #{str}", GPI::CLU.check_option('v')
    if str == v
      GPI.print "Image found", GPI::CLU.check_option('v')
      @@search = false
      return set
    end
  end
  @@search = false
  nil
end

.is_valid?(filename) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/zarchitect/image.rb', line 120

def self.is_valid?(filename)
  [".png",".gif",".jpg",".jpeg",".bmp"].include?(File.extname(filename))
end

Instance Method Details

#create_thumbnail(path, thumb_x, thumb_y) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/zarchitect/image.rb', line 82

def create_thumbnail(path, thumb_x, thumb_y)
  GPI.print "attempting to create thumbnail #{path}",
    GPI::CLU.check_option('v')
  return false if path.include?("/#{SHARESDIR}/") # no thumbs for pic within
  x = @dimensions.x
  y = @dimensions.y
  if x <= thumb_x && y <= thumb_y # no need to create thumbnail
    GPI.print "abort thumbnail creation. No thumbnail #{thumb_x}x"\
      "#{thumb_y} necessary", GPI::CLU.check_option('v')
    return false
  end
  if ["PNG", "GIF"].include?(@type)
    # scale
    while true do
      x = x/2
      y = y/2
      break if x <= thumb_x && y <= thumb_y
    end
    if x < 1 || y < 1 
      GPI.print "failed to create #{path}: invalid downsizing",
        GPI::CLU.check_option('v')
      return false
    end
    command = "convert #{@path} -scale #{x}x#{y} #{path}"
    GPI.print "#{command}", GPI::CLU.check_option('v')
    o = %x{#{command}}
    GPI.print o, GPI::CLU.check_option('v')
    return true
  else
    # resize
    command = "convert #{@path} -resize #{thumb_x}x#{thumb_y} #{path}"
    GPI.print "#{command}", GPI::CLU.check_option('v')
    o = %x{#{command}}
    GPI.print o, GPI::CLU.check_option('v')
    return true
  end
end

#larger_than_thumb_large?Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/zarchitect/image.rb', line 77

def larger_than_thumb_large?
  @dimensions.x > Zarchitect.conf.thumbl[0].to_i ||
    @dimensions.y > Zarchitect.conf.thumbl[1].to_i
end

#larger_than_thumb_small?Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/zarchitect/image.rb', line 72

def larger_than_thumb_small?
  @dimensions.x > Zarchitect.conf.thumbs[0].to_i ||
    @dimensions.y > Zarchitect.conf.thumbs[1].to_i
end

#thumb_large?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/zarchitect/image.rb', line 57

def thumb_large?
  @thumbl_f
end

#thumb_small?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/zarchitect/image.rb', line 53

def thumb_small?
  @thumbs_f
end

#urlObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/zarchitect/image.rb', line 61

def url
=begin
  if (!(Page.current_page.nil?) && Page.current_page.draft && !@@search)
    File.join("..", DRAFTDIR, @url)
  else
    @url
  end
=end
  @url
end