Class: Prawn::Images::Image

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

Direct Known Subclasses

JPG, PNG

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.detect_image_format(content) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/prawn/images/image.rb', line 47

def self.detect_image_format(content)
  top = content[0,128]                       

  # Unpack before comparing for JPG header, so as to avoid having to worry
  # about the source string encoding. We just want a byte-by-byte compare.
  if top[0, 3].unpack("C*") == [255, 216, 255]
    return :jpg
  elsif top[0, 8].unpack("C*") == [137, 80, 78, 71, 13, 10, 26, 10]
    return :png
  else
    raise Errors::UnsupportedImageType, "image file is an unrecognised format"
  end
end

Instance Method Details

#calc_image_dimensions(options) ⇒ Object



14
15
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
# File 'lib/prawn/images/image.rb', line 14

def calc_image_dimensions(options)
  w = options[:width] || width
  h = options[:height] || height

  if options[:width] && !options[:height]
    wp = w / width.to_f 
    w = width * wp
    h = height * wp
  elsif options[:height] && !options[:width]         
    hp = h / height.to_f
    w = width * hp
    h = height * hp   
  elsif options[:scale] 
    w = width * options[:scale]
    h = height * options[:scale]
  elsif options[:fit] 
    bw, bh = options[:fit]
    bp = bw / bh.to_f
    ip = width / height.to_f
    if ip > bp
      w = bw
      h = bw / ip
    else
      h = bh
      w = bh * ip
    end
  end
  self.scaled_width = w
  self.scaled_height = h

  [w,h]
end