Class: CortexReaver::Photograph

Inherits:
Object
  • Object
show all
Defined in:
lib/cortex_reaver/model/photograph.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.atom_urlObject



16
17
18
# File 'lib/cortex_reaver/model/photograph.rb', line 16

def self.atom_url
  '/photographs/atom'
end

.get(id) ⇒ Object



20
21
22
# File 'lib/cortex_reaver/model/photograph.rb', line 20

def self.get(id)
  self[:name => id] || self[id]
end

.recentObject



24
25
26
# File 'lib/cortex_reaver/model/photograph.rb', line 24

def self.recent
  reverse_order(:created_on).limit(16)
end

.regenerate_sizesObject



28
29
30
31
32
# File 'lib/cortex_reaver/model/photograph.rb', line 28

def self.regenerate_sizes
  all.each do |p|
    p.regenerate_sizes
  end
end

.urlObject



34
35
36
# File 'lib/cortex_reaver/model/photograph.rb', line 34

def self.url
  '/photographs'
end

Instance Method Details

#atom_urlObject



38
39
40
# File 'lib/cortex_reaver/model/photograph.rb', line 38

def atom_url
  '/photographs/atom/' + name
end

#dateObject

Gives the best guess for this photograph’s date.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cortex_reaver/model/photograph.rb', line 48

def date
  if exif = self.exif
    fields = [:date_time_original, :date_time_digitized, :date_time]
    fields.each do |field|
      begin
        if date = exif.send(field)
          return date
        end
      rescue
      end
    end
  end

  # Fall back on creation date
  self.created_on
end

#exifObject

Returns the exif data for this photograph



43
44
45
# File 'lib/cortex_reaver/model/photograph.rb', line 43

def exif
  EXIFR::JPEG.new(self.full_local_path).exif
end

#full_local_pathObject

Returns the system path to the full photograph



88
89
90
# File 'lib/cortex_reaver/model/photograph.rb', line 88

def full_local_path
  path :local, 'medium'
end

#full_public_pathObject

Returns the path to the full photograph



93
94
95
# File 'lib/cortex_reaver/model/photograph.rb', line 93

def full_public_path
  path :public, 'medium'
end

#image=(file) ⇒ Object

Store an photograph on disk in the appropriate sizes.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cortex_reaver/model/photograph.rb', line 66

def image=(file)
  if file.blank? or file.size == 0
    return nil
  end

  # Write file to disk
  attachment = Attachment.new(self, 'original.jpg').file = file

  # Compute thumbnails
  regenerate_sizes

  true
end

#infer_date_from_exif!Object

Sets the date from the EXIF date tag.



81
82
83
84
85
# File 'lib/cortex_reaver/model/photograph.rb', line 81

def infer_date_from_exif!
  if exif = self.exif
    self.created_on = self.date
  end
end

#path(type = :local, size = :full) ⇒ Object

The path to a photograph



98
99
100
# File 'lib/cortex_reaver/model/photograph.rb', line 98

def path(type = :local, size = :full)
  attachment(size.to_s + '.jpg').path(type)
end

#regenerate_sizesObject

Regenerates various photo sizes.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/cortex_reaver/model/photograph.rb', line 103

def regenerate_sizes
  sizes = CortexReaver.config.photographs.sizes
  Ramaze::Log.info "Regenerating photo sizes for #{self}"

  # Find existing attachments, in order of decreasing (roughly) size
  known_files = attachments.map{|a| a.name.sub(/\.jpg$/,'')} & (sizes.keys.map(&:to_s) + ['original'])
  largest = attachment(
    known_files.sort_by { |f| 
      File.stat(attachment("#{f}.jpg").path).size
    }.last + '.jpg'
  )

  # Replace original.jpg with the largest available, if necessary.
  orig = attachment 'original.jpg'
  unless orig.exists? and orig == largest
    orig.file = largest
  end

  # Delete everything but original.jpg
  attachments.reject {|a| a.name == 'original.jpg'}.each{|a| a.delete}

  # Read image through ImageMagick
  begin
    image = Magick::Image.read(orig.local_path).first
  rescue => e
    Ramaze::Log.error "Invalid image #{orig.local_path}; not processing."
    return false
  end

  # Write appropriate sizes to disk
  sizes.each do |size, geometry|
    attachment = attachment(size.to_s + '.jpg')
    
    if size == :grid
      resized = image.resize_to_fill(*geometry.split('x').map(&:to_i))
    else
      image.change_geometry(geometry) do |width, height|
        resized = image.scale(width, height)
      end
    end
    
    resized.write attachment.local_path
  end
    
  # Free IM stubs
  image = nil
  resized = nil
  GC.start

  true
end

#thumbnail_local_pathObject

Returns the system path to the thumbnail photograph



156
157
158
# File 'lib/cortex_reaver/model/photograph.rb', line 156

def thumbnail_local_path
  path :local, 'thumbnail'
end

#thumbnail_public_pathObject

Returns the path to the thumbnail photograph



161
162
163
# File 'lib/cortex_reaver/model/photograph.rb', line 161

def thumbnail_public_path
  path :public, 'thumbnail'
end

#to_sObject



165
166
167
# File 'lib/cortex_reaver/model/photograph.rb', line 165

def to_s
  title || name
end

#urlObject



169
170
171
# File 'lib/cortex_reaver/model/photograph.rb', line 169

def url
  '/photographs/show/' + name
end

#validateObject



173
174
175
176
177
178
# File 'lib/cortex_reaver/model/photograph.rb', line 173

def validate
  validates_unique :name
  validates_presence :name
  validates_max_length 255, :name
  validates_presence :title
end