Class: Magick::Image

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

Instance Method Summary collapse

Instance Method Details

#exifObject



25
26
27
# File 'lib/iphone_polaroid.rb', line 25

def exif
  @exif ||= EXIFR::JPEG.new(StringIO.new(to_blob))
end

#latObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/iphone_polaroid.rb', line 29

def lat
  unless instance_variable_defined?(:@lat)
    lat = exif.gps_latitude
    unless lat.nil?
      lat = lat[0] + lat[1] / 60.0 + lat[2] / 3600.0
      lat *= -1 if exif.gps_longitude_ref == 'S'
    end
    @lat = lat
  end
  @lat
end

#lonObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/iphone_polaroid.rb', line 41

def lon
  unless instance_variable_defined?(:@lon)
    lon = exif.gps_longitude
    unless lon.nil?
      lon = lon[0] + lon[1] / 60.0 + lon[2] / 3600.0
      lon *= -1 if exif.gps_longitude_ref == 'W'
    end
    @lon = lon
  end
  @lon
end

#polaroid(options = {}) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/iphone_polaroid.rb', line 53

def polaroid(options={})
  o = {
    :photo_width => 216,
    :photo_height => 225,
    :border_color => '#e8eef3',
    :border_width => 18,
    :bottom_border_width => 63,

    :text_fill => '#000000',
    :text_font => 'Amaze-Normal',
    :text_gravity => Magick::SouthGravity,
    :text_size => 20,
    :text_stroke => 'transparent',

    :amplitude => 0.01,
    :shadow_color => 'gray75',
    :shadow_blur_radius => 0,
    :shadow_blur_sigma => 3,
    :shadow_composite_y => 5,
    :rotate => -5,
    }.merge(options)

  img = copy

  img.resize_to_fill!(o[:photo_width], o[:photo_height])

  border = Magick::GradientFill.new(0, 0, 0, 0, o[:border_color],
    o[:border_color])
  border = Magick::Image.new(img.columns + o[:border_width] * 2,
    img.rows + o[:border_width] + o[:bottom_border_width], border)
  img = border.composite(img, Magick::NorthWestGravity, o[:border_width],
    o[:border_width], Magick::OverCompositeOp)

  caption = exif.date_time.strftime('%m/%d/%Y').sub(/(^|\/)0/, '\1')

  unless lat.nil? or lon.nil?
    caption =
      "#{caption} #{IPhonePolaroid.lat_lon_to_city_state(lat, lon)}"
  end

  text = Magick::Draw.new
  text.annotate(img, 0, 0, 0, o[:border_width], caption) {
    self.fill = o[:text_fill]
    self.font = o[:text_font]
    self.gravity = o[:text_gravity]
    self.pointsize = o[:text_size]
    self.stroke = o[:text_stroke]
  }

  img.background_color = 'none'
  amplitude = img.columns * o[:amplitude]
  wavelength = img.rows  * 2
  img.rotate!(90)
  img = img.wave(amplitude, wavelength)
  img.rotate!(-90)

  shadow = img.flop
  shadow = shadow.colorize(1, 1, 1, o[:shadow_color])
  shadow.background_color = 'white'
  shadow.border!(10, 10, 'white')
  shadow = shadow.blur_image(o[:shadow_blur_radius], o[:shadow_blur_sigma])
  img = shadow.composite(img, -amplitude / 2, o[:shadow_composite_y],
    Magick::OverCompositeOp)
  img.rotate!(o[:rotate])
  img.trim!

  img

end