Class: Photoapp::Photo

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/photoapp/photo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, logo, session) ⇒ Photo

Returns a new instance of Photo.



8
9
10
11
12
13
# File 'lib/photoapp/photo.rb', line 8

def initialize(file, , session)
  @file = file
   = 
  @session = session
  @config = session.config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/photoapp/photo.rb', line 6

def config
  @config
end

#fileObject

Returns the value of attribute file.



6
7
8
# File 'lib/photoapp/photo.rb', line 6

def file
  @file
end

#imageObject

Returns the value of attribute image.



6
7
8
# File 'lib/photoapp/photo.rb', line 6

def image
  @image
end

#logoObject

Returns the value of attribute logo.



6
7
8
# File 'lib/photoapp/photo.rb', line 6

def 
  
end

#sessionObject

Returns the value of attribute session.



6
7
8
# File 'lib/photoapp/photo.rb', line 6

def session
  @session
end

Instance Method Details

#add_shadowed_text(text, size, gravity, width = 800) ⇒ Object



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/photoapp/photo.rb', line 50

def add_shadowed_text(text, size, gravity, width=800)
  setting = config
  shadowed_text = []

  %w(#000 #fff).each do |color|
    image = Image.new(width, 70) { self.background_color = "rgba(255, 255, 255, 0" }
    txt = Draw.new
    txt.annotate(image, 0, 0, 0, 0, text) do
      txt.gravity = gravity
      txt.pointsize = size
      txt.fill = color
      txt.font = setting['font']

      if color == '#000'
        txt.stroke = color
      end
    end

    # Blur drop shadow
    if color == '#000'
      image = image.blur_image(radius=6.0, sigma=2.0)
    end

    shadowed_text.push image
  end

  shadowed_text
end

#cleanupObject



108
109
110
111
# File 'lib/photoapp/photo.rb', line 108

def cleanup
  watermark.destroy!
  with_url.destroy!
end

#date_textObject



45
46
47
48
# File 'lib/photoapp/photo.rb', line 45

def date_text
  date = Time.now.strftime('%b %d, %Y')
  add_shadowed_text date, config['date_font_size'], NorthEastGravity, 250
end

#import_destObject



127
128
129
# File 'lib/photoapp/photo.rb', line 127

def import_dest
  File.join(config['photos_import'], short + '.jpg')
end


117
118
119
# File 'lib/photoapp/photo.rb', line 117

def print_dest
  File.join(config['print'], short + '.jpg')
end


121
122
123
124
125
# File 'lib/photoapp/photo.rb', line 121

def print_dup_dest
  if config['print_duplicate']
    File.join(config['print_duplicate'], short + '.jpg')
  end
end

#shortObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/photoapp/photo.rb', line 131

def short
  @short ||= begin
    now = Time.now
    date = "#{now.strftime('%y')}#{now.strftime('%d')}#{now.month}"
    source = [*?a..?z] - ['o', 'l'] + [*2..9]
    short = ''
    5.times { short << source.sample.to_s }
    short = "#{short}#{date}"
    session.photos << short + '.jpg'
    short
  end
end

#upload_destObject



113
114
115
# File 'lib/photoapp/photo.rb', line 113

def upload_dest
  File.join(config['upload'], short + '.jpg')
end

#url_textObject



41
42
43
# File 'lib/photoapp/photo.rb', line 41

def url_text
  add_shadowed_text "#{config['url_base']}/#{short}.jpg", config['font_size'], NorthEastGravity
end

#watermarkObject



23
24
25
26
27
28
29
30
# File 'lib/photoapp/photo.rb', line 23

def watermark
  @watermarked ||= begin
    date = date_text
    image.composite(, SouthWestGravity, 50, 40, OverCompositeOp)
      .composite(date[0], SouthWestGravity, 5, -10, OverCompositeOp)
      .composite(date[1], SouthWestGravity, 5, -10, OverCompositeOp)
  end
end

#with_urlObject



32
33
34
35
36
37
38
39
# File 'lib/photoapp/photo.rb', line 32

def with_url
  @printable ||= begin
    url = url_text
    watermark.dup
      .composite(url[0], SouthEastGravity, 40, 30, OverCompositeOp)
      .composite(url[1], SouthEastGravity, 40, 30, OverCompositeOp)
  end
end

#write(path = nil) ⇒ Object



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
# File 'lib/photoapp/photo.rb', line 79

def write(path=nil)
  if path
    FileUtils.mkdir_p(File.dirname(path))
    with_url.write path
    cleanup
  else
    FileUtils.mkdir_p(File.dirname(upload_dest))
    FileUtils.mkdir_p(File.dirname(print_dest))
    FileUtils.mkdir_p(File.dirname(print_dup_dest)) if print_dup_dest
    FileUtils.mkdir_p(File.dirname(import_dest))

    puts "writing #{upload_dest}"
    watermark.write upload_dest

    puts "writing #{print_dest}"
    with_url.write print_dest

    if print_dup_dest
      puts "writing #{print_dup_dest}"
      with_url.write print_dup_dest
    end

    puts "writing #{import_dest}"
    with_url.write import_dest

    cleanup
  end
end