Class: CortexReaver::Photograph
- Inherits:
-
Object
- Object
- CortexReaver::Photograph
- Defined in:
- lib/cortex_reaver/model/photograph.rb
Class Method Summary collapse
Instance Method Summary collapse
- #atom_url ⇒ Object
-
#date ⇒ Object
Gives the best guess for this photograph’s date.
-
#exif ⇒ Object
Returns the exif data for this photograph.
-
#full_local_path ⇒ Object
Returns the system path to the full photograph.
-
#full_public_path ⇒ Object
Returns the path to the full photograph.
-
#image=(file) ⇒ Object
Store an photograph on disk in the appropriate sizes.
-
#infer_date_from_exif! ⇒ Object
Sets the date from the EXIF date tag.
-
#path(type = :local, size = :full) ⇒ Object
The path to a photograph.
-
#regenerate_sizes ⇒ Object
Regenerates various photo sizes.
-
#thumbnail_local_path ⇒ Object
Returns the system path to the thumbnail photograph.
-
#thumbnail_public_path ⇒ Object
Returns the path to the thumbnail photograph.
- #to_s ⇒ Object
- #url ⇒ Object
- #validate ⇒ Object
Class Method Details
.atom_url ⇒ Object
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 |
.recent ⇒ Object
24 25 26 |
# File 'lib/cortex_reaver/model/photograph.rb', line 24 def self.recent reverse_order(:created_on).limit(16) end |
.regenerate_sizes ⇒ Object
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 |
.url ⇒ Object
34 35 36 |
# File 'lib/cortex_reaver/model/photograph.rb', line 34 def self.url '/photographs' end |
Instance Method Details
#atom_url ⇒ Object
38 39 40 |
# File 'lib/cortex_reaver/model/photograph.rb', line 38 def atom_url '/photographs/atom/' + name end |
#date ⇒ Object
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 |
#exif ⇒ Object
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_path ⇒ Object
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_path ⇒ Object
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.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) (size.to_s + '.jpg').path(type) end |
#regenerate_sizes ⇒ Object
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 = .map{|a| a.name.sub(/\.jpg$/,'')} & (sizes.keys.map(&:to_s) + ['original']) largest = ( known_files.sort_by { |f| File.stat(("#{f}.jpg").path).size }.last + '.jpg' ) # Replace original.jpg with the largest available, if necessary. orig = 'original.jpg' unless orig.exists? and orig == largest orig.file = largest end # Delete everything but original.jpg .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| = (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 .local_path end # Free IM stubs image = nil resized = nil GC.start true end |
#thumbnail_local_path ⇒ Object
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_path ⇒ Object
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_s ⇒ Object
165 166 167 |
# File 'lib/cortex_reaver/model/photograph.rb', line 165 def to_s title || name end |
#url ⇒ Object
169 170 171 |
# File 'lib/cortex_reaver/model/photograph.rb', line 169 def url '/photographs/show/' + name end |
#validate ⇒ Object
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 |