Class: GD::Image

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/gd/image.rb

Direct Known Subclasses

IndexedColorImage, TrueColorImage

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(struct) ⇒ Image

Returns a new instance of Image.

Raises:

  • (ArgumentError)


43
44
45
46
# File 'lib/gd/image.rb', line 43

def initialize(struct)
  raise ArgumentError, 'No struct given' unless struct
  @struct = struct
end

Class Method Details

.import(filename, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gd/image.rb', line 8

def import(filename, options = {})
  format = options.delete(:format)
  format = format.to_s.downcase if format
  raise ArgumentError, "Unsupported format '#{format}'" if format && !FORMATS.include?(format)
  format ||= format_from_filename(filename) || format_from_magic(File.binread(filename, 4))
  method = { PNG => :gdImageCreateFromPng, JPEG => :gdImageCreateFromJpeg, GIF => :gdImageCreateFromGif }[format]
  if ptr = File.open(filename, 'rb') { |f| GD::LIB.public_send(method, f) } and not ptr.null?
    ptr.free = GD::LIB['gdImageDestroy']
    struct = LIB::ImageStruct.new(ptr)
    if struct.trueColor.zero?
      IndexedColorImage.new(struct)
    else
      TrueColorImage.new(struct)
    end
  end
end

Instance Method Details

#heightObject



56
57
58
# File 'lib/gd/image.rb', line 56

def height
  struct.sy
end

#true_color?Boolean

Returns:

  • (Boolean)


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

def true_color?
  not struct.trueColor.zero?
end

#widthObject



52
53
54
# File 'lib/gd/image.rb', line 52

def width
  struct.sx
end