class GD::Image

Public Class Methods

import(filename, options = {}) click to toggle source
# 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
new(struct) click to toggle source
# File lib/gd/image.rb, line 43
def initialize(struct)
  raise ArgumentError, 'No struct given' unless struct
  @struct = struct
end

Private Class Methods

format_from_filename(filename) click to toggle source
# File lib/gd/image.rb, line 27
def format_from_filename(filename)
  extension = File.extname(filename).downcase[1..-1]
  extension = JPEG if 'jpg' == extension
  extension if FORMATS.include?(extension)
end
format_from_magic(data) click to toggle source
# File lib/gd/image.rb, line 33
def format_from_magic(data)
  case data
  when /\A\xFF\xD8/n then JPEG
  when /\A\x89PNG/n  then PNG
  when /\AGIF8/n     then GIF
  end
end

Public Instance Methods

height() click to toggle source
# File lib/gd/image.rb, line 56
def height
  struct.sy
end
true_color?() click to toggle source
# File lib/gd/image.rb, line 48
def true_color?
  not struct.trueColor.zero?
end
width() click to toggle source
# File lib/gd/image.rb, line 52
def width
  struct.sx
end