Class: ImageSize

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

Defined Under Namespace

Modules: Type

Constant Summary collapse

JpegCodeCheck =
[
  "\xc0", "\xc1", "\xc2", "\xc3",
  "\xc5", "\xc6", "\xc7",
  "\xc9", "\xca", "\xcb",
  "\xcd", "\xce", "\xcf",
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(img_data, img_type = nil) ⇒ ImageSize

receive image & make size argument is image String, StringIO or IO



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/image_size.rb', line 39

def initialize(img_data, img_type = nil)
  @img_data = img_data.dup
  @img_wedth = nil
  @img_height = nil
  @img_type = nil

  if @img_data.is_a?(IO)
    img_top = @img_data.read(1024)
    img_io = def_read_o(@img_data)
  elsif @img_data.is_a?(StringIO)
    img_top = @img_data.read(1024)
    img_io = def_read_o(@img_data)
  elsif @img_data.is_a?(String)
    img_top = @img_data[0, 1024]
#      img_io = StringIO.open(@img_data){|sio| io = def_read_o(sio); io }
    img_io = StringIO.open(@img_data)
    img_io = def_read_o(img_io)
  else
    raise "argument class error!! #{img_data.type}"
  end
  
  if @img_type.nil?
    @img_type = check_type(img_top)
  else
    type = Type.constants.find{|t| img_type == t }
    raise("type is failed. #{img_type}\n") if !type
    @img_type = img_type
  end

  if @img_type != Type::OTHER
    @img_width, @img_height = self.__send__("measure_#{@img_type}", img_io)
  else
    @img_width, @img_height = [nil, nil]
  end
  
  if @img_data.is_a?(String)
    img_io.close
  end
end

Class Method Details

.type_listObject

image type list



33
34
35
# File 'lib/image_size.rb', line 33

def ImageSize.type_list
  Type.constants 
end

Instance Method Details

#get_heightObject Also known as: height, h

get image height



84
85
86
# File 'lib/image_size.rb', line 84

def get_height
  if @img_type == Type::OTHER then nil else @img_height end
end

#get_sizeObject Also known as: size

get image wight and height(Array)



94
95
96
# File 'lib/image_size.rb', line 94

def get_size
  [self.get_width, self.get_height]
end

#get_typeObject

get image type ex. “GIF”, “PNG”, “JPEG”



81
# File 'lib/image_size.rb', line 81

def get_type; @img_type; end

#get_widthObject Also known as: width, w

get image height



89
90
91
# File 'lib/image_size.rb', line 89

def get_width
  if @img_type == Type::OTHER then nil else @img_width end
end