Class: Dimension::Image

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

Constant Summary collapse

GRAVITIES =
{
  'nw' => 'NorthWest',
  'n'  => 'North',
  'ne' => 'NorthEast',
  'w'  => 'West',
  'c'  => 'Center',
  'e'  => 'East',
  'sw' => 'SouthWest',
  's'  => 'South',
  'se' => 'SouthEast'
}
RESIZE_GEOMETRY =

Geometry string patterns

/^(\d+)?x(\d+)?[><%^!]?$|^\d+@$/
CROPPED_RESIZE_GEOMETRY =

e.g. ‘20x50:ne’

/^(\d+)x(\d+)[:|@](\w{1,2})?$/
CROP_GEOMETRY =

e.g. ‘30x30+10+10’

/^(\d+)x(\d+)([+-]\d+)?([+-]\d+)?(\w{1,2})?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Image

Returns a new instance of Image.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dimension/image.rb', line 26

def initialize(file)
  unless Dimension.processor
    raise "No processor set! Please set Dimension.processor = 'imlib2' or 'image_magick'"
  end

  @file = File.expand_path(file)
  @name = File.basename(file)
  @path = File.dirname(@file)

  raise ArgumentError, "File not found: #{@file}" unless File.exist?(@file)
  log "File loaded: #{file}. Geometry: #{geometry.join('x')}"
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



24
25
26
# File 'lib/dimension/image.rb', line 24

def file
  @file
end

Instance Method Details

#generate(geometry, &block) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/dimension/image.rb', line 39

def generate(geometry, &block)
  new_geometry = resize_to(geometry)
  if block_given?
    out = yield(new_geometry)
    close
  end
  out || self
end

#generate!(geometry, output_file = nil) ⇒ Object



48
49
50
51
52
# File 'lib/dimension/image.rb', line 48

def generate!(geometry, output_file = nil)
  @geometry_string = geometry # for saving as [file]-[geometry].[ext]
  resize_to(geometry)
  save(output_file) && self
end

#inspectObject



112
113
114
115
# File 'lib/dimension/image.rb', line 112

def inspect
  # geometry = get_new_geometry
  "#<Dimension::Image:#{object_id}>" # @width=#{geometry[0]}, @height=#{geometry[1]}>"
end

#log(msg) ⇒ Object



121
122
123
# File 'lib/dimension/image.rb', line 121

def log(msg)
  puts "[Dimension::#{Dimension.processor}] #{msg}"
end

#resize_to(geometry) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dimension/image.rb', line 54

def resize_to(geometry)
  case geometry
    when RESIZE_GEOMETRY
      log "Resize -- #{$1}x#{$2}"
      resize($1, $2)
    when CROPPED_RESIZE_GEOMETRY
      log "Resize and crop -- width: #{$1}, height: #{$2}, gravity: #{$3}"
      resize_and_crop($1, $2, $3)
    when CROP_GEOMETRY
      log "Crop -- width: #{$1}, height: #{$2}, x: #{$3}, y: #{$4}, gravity: #{$5}"
      crop($1, $2, $3, $4, $5)
    else
      raise ArgumentError, "Didn't recognise the geometry string #{geometry}"
  end

  new_geometry = get_new_geometry
  {:width => new_geometry[0], :height => new_geometry[1]}
end

#save(out_file) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dimension/image.rb', line 73

def save(out_file)
  new_geometry = get_new_geometry
  path = @path

  if out_file and File.directory?(out_file)
    path = File.expand_path(out_file)
    out_file = nil
  end

  if out_file.nil?
    out_file = File.join(path, @name.sub(File.extname(file), '-' + @geometry_string + File.extname(file)))
  end

  log "Writing file: #{out_file}"
  save_as(out_file) or return false
  close

  {:file => out_file, :width => new_geometry[0], :height => new_geometry[1]}
end

#to_aObject



97
98
99
# File 'lib/dimension/image.rb', line 97

def to_a
  to_response
end

#to_response(env = nil) ⇒ Object



117
118
119
# File 'lib/dimension/image.rb', line 117

def to_response(env = nil)
  [200, {'Content-Type' => "image/#{format}"}, [image_data]]
end

#to_rgbObject

transforms data (RGBA buffer) into a array of RGB values



107
108
109
110
# File 'lib/dimension/image.rb', line 107

def to_rgb
  bytes = image_data.bytes
  (1..bytes.length).step(4).map { |i| [bytes[i-1],bytes[i],bytes[i+1]] }.flatten
end

#to_rgbaObject



101
102
103
104
# File 'lib/dimension/image.rb', line 101

def to_rgba
  bytes = image_data.bytes
  (1..bytes.length).step(4).map { |i| bytes[i..i+2] << bytes[i-1] }.flatten
end

#to_sObject



93
94
95
# File 'lib/dimension/image.rb', line 93

def to_s
  image_data
end