recreate thumbs. css. thumb local for photo

rails2
Espen Antonsen 2009-06-12 01:04:57 +02:00
parent c93ac2ad2c
commit aa30654071
14 changed files with 74 additions and 40 deletions

View File

@ -1,6 +1,7 @@
class CollectionsController < ApplicationController
before_filter :require_role_admin, :only => [:new, :create, :edit, :update, :destroy]
before_filter :require_role_admin, :only => [:new, :create, :edit, :update, :destroy]
def index
@collections = Collection.find(:all)
respond_to do |format|
@ -35,11 +36,11 @@ class CollectionsController < ApplicationController
end
def edit
@collection = Collection.find( params[:id])
@collection = Collection.find_by_title( params[:id])
end
def update
@collection = Collection.find( params[:id])
@collection = Collection.find_by_title( params[:id])
if @collection.update_attributes(params[:collection])
flash[:notice] = "Collection updated!"
redirect_to @collection
@ -49,7 +50,7 @@ class CollectionsController < ApplicationController
end
def destroy
@collection = Collection.find( params[:id])
@collection = Collection.find_by_title( params[:id])
if @collection.destroy
redirect_to collections_path
else

View File

@ -4,8 +4,9 @@ class Album < ActiveRecord::Base
has_many :collections, :through => :collection_albums
validates_uniqueness_of :path, :message => "Album already exsists on disc"
before_validation :ensure_path
validates_presence_of :title, :message => "can't be blank"
before_validation :ensure_path, :set_title
after_create :create_folders
after_destroy :destroy_folders
@ -21,7 +22,11 @@ class Album < ActiveRecord::Base
def ensure_path
self.path = self.title if !self.path
self.path = self.title unless self.path
end
def set_title
self.title = File.basename( File.dirname(self.path) ) unless self.title || !self.path
end
def tag_list
@ -77,13 +82,13 @@ class Album < ActiveRecord::Base
private
def create_folders
Dir.mkdir( APP_CONFIG[:photos_path] + self.path ) if !File.exists?( APP_CONFIG[:photos_path] + self.path )
Dir.mkdir( APP_CONFIG[:thumbs_path] + self.path ) if !File.exists?( APP_CONFIG[:photos_path] + self.path )
Dir.mkdir( APP_CONFIG[:photos_path] + self.path ) unless File.exists?( APP_CONFIG[:photos_path] + self.path )
Dir.mkdir( APP_CONFIG[:thumbs_path] + self.path ) unless File.exists?( APP_CONFIG[:thumbs_path] + self.path )
end
def destroy_folders
#puts "DELETE DIRECTORY " + APP_CONFIG[:photos_path] + self.path
Dir.delete( APP_CONFIG[:thumbs_path] + self.path ) if File.exists?( APP_CONFIG[:thumbs_path] + self.path )
Dir.delete( APP_CONFIG[:photos_path] + self.path ) if File.exists?( APP_CONFIG[:photos_path] + self.path )
Dir.delete( APP_CONFIG[:thumbs_path] + self.path ) if File.exists?( APP_CONFIG[:thumbs_path] + self.path )
end
end

View File

@ -1,9 +1,17 @@
class Collection < ActiveRecord::Base
has_many :collection_albums
has_many :albums, :through => :collection_albums
#accepts_nested_attributes_for :albums, :allow_destroy => true
attr_accessor :album_list
def to_param
title.gsub(/[^a-z0-9]+/i, '-')
end
def album_list=(albums)
self.albums = Album.find(albums)
end
end

View File

@ -9,6 +9,7 @@ class Photo < ActiveRecord::Base
validates_uniqueness_of :path, :message => "Photo already exsists on disc"
validates_presence_of :title
before_validation :set_title
before_create :exif_read
after_create :create_thumbnails
before_update :exif_write # should only write if tags are changed as images can be large and thus ExifTool will take a while to write to the file
@ -25,6 +26,10 @@ class Photo < ActiveRecord::Base
id.to_s + '-' + title.gsub(/[^a-z0-9]+/i, '-')
end
def set_title
self.title = File.basename( File.dirname(path) ).gsub( self.extension, "" ) unless self.title
end
def path_original_public
return APP_CONFIG[:photos_path_public] + self.path
end
@ -79,6 +84,24 @@ class Photo < ActiveRecord::Base
File.open(APP_CONFIG[:photos_path] + self.path, "wb") { |f| f.write(data.read) }
end
def create_thumbnails
ImageScience.with_image(APP_CONFIG[:photos_path] + self.path) do |img|
img.cropped_thumbnail(200) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_collection" + self.extension
end
img.cropped_thumbnail(100) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_album" + self.extension
end
img.thumbnail(210) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_preview" + self.extension
end
img.thumbnail(950) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_single" + self.extension
end
end
end
protected
@ -93,27 +116,9 @@ class Photo < ActiveRecord::Base
def path_modified(size)
return APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_" + size + self.extension
end
private
def create_thumbnails
ImageScience.with_image(APP_CONFIG[:photos_path] + self.path) do |img|
#puts " thumbing it..thumbing it.."
ext = File.extname( APP_CONFIG[:photos_path] + self.path )
img.thumbnail(85) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_thumb" + ext
end
img.thumbnail(150) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_album" + ext
end
img.thumbnail(800) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_large" + ext
end
end
end
def exif_read
photo = MiniExiftool.new(self.path_original)
self.longitude = photo.GPSLongitude if self.longitude.nil?

View File

@ -1 +1 @@
<p><%= render :partial => "photos/thumb", :locals => { :photo => album.photos.find(:first) } unless album.photos.empty? %></p>
<p><%= render :partial => "photos/thumb", :locals => { :photo => album.photos.find(:first), :photosize => 'collection' } unless album.photos.empty? %></p>

View File

@ -6,7 +6,7 @@
<% for photo in @album.photos.find(:all, :order => "Id ASC") %>
<% count += 1%>
<% if count == 1 || ( (count-1) / 4.0 == ( (count-1) / 4.0).to_i ) %><tr><% end %>
<td><%= link_to image_tag( photo.path_modified_public("album") ), [@album.collections.first, @album, photo] %></td>
<td><%= link_to image_tag( photo.path_modified_public("preview") ), [@album.collections.first, @album, photo] %></td>
<% if count / 4.0 == (count / 4.0).to_i %></tr><% end %>
<% end %>
</table>

View File

@ -3,4 +3,9 @@
<%= form.label :description %><br />
<%= form.text_area :description %><br />
<%= form.label :album %><br />
<%= collection_select :collection, :album_list, Album.find(:all), :id, :title, {:selected => @collection.albums.map{|album|album.id} }, {:multiple => true, :rows => 10} %><br />
<br />

View File

@ -6,7 +6,7 @@
<%= f.submit "Update" %>
<% end %>
<br /><%= link_to("Delete collection", { :action => "destroy", :id => @collection },
<br /><%= link_to("Delete this collection", { :action => "destroy", :id => @collection },
:confirm => "Are you sure you want to delete this collection?",
:method => :delete) %>
<br /><%= link_to "All collections", collections_path %>

View File

@ -3,7 +3,11 @@
<% for collection in @collections %>
<div class="thumb">
<h3><%= link_to collection.title, collection %></h3>
<%= render :partial => collection.albums.find(:first) %>
<%= render :partial => collection.albums.find(:first), :locals => {:photosize => 'collection'} %>
</div>
<% end %>
</div>
</div>
<% if has_role?("admin") %>
<br /><%= link_to "New collection", new_collection_path %>
<% end %>

View File

@ -4,7 +4,7 @@
<% for album in @collection.albums %>
<div class="row">
<div class="title">
<%= render :partial => "photos/thumb", :locals => {:photo => album.photos.first } %>
<%= render :partial => "photos/thumb", :locals => {:photo => album.photos.first } unless album.photos.empty? %>
<p><%= link_to album.title, collection_album_path(@collection, album) %></p>
</div>
<div class="image">

View File

@ -1 +1 @@
<%= link_to ( image_tag photo.path_modified_public("album"), { :id => 'thumb_' + photo.id.to_s } ), photo_path(photo) %>
<%= link_to (image_tag photo.path_modified_public(defined?(photosize) ? photosize : "album") ), photo_path(photo) %>

View File

@ -14,7 +14,7 @@
<% end %>
</p>
</div>
<p><%= image_tag @photo.path_modified_public("large") %></p>
<p><%= image_tag @photo.path_modified_public("single") %></p>
</div>
<br/>

View File

@ -18,12 +18,12 @@ module ScanFiles
end
if album.nil?
puts "New album : " + File.basename( relpath )
album = Album.create( :path => relpath, :title => File.basename( File.dirname(path) ) )
album = Album.create( :path => relpath )
end
photo = Photo.find_by_path( relfile )
if photo.nil?
puts " New photo added " + relfile
photo = Photo.create( :album => album, :title => File.basename(path).sub( File.extname(path), '' ) , :path => relfile )
photo = Photo.create( :album => album, :path => relfile )
else
puts " Found photo " + relfile
end
@ -31,6 +31,12 @@ module ScanFiles
}
end
def self.RecreateThumbnails
Photo.find(:all).each {|photo|
photo.create_thumbnails()
}
end
def self.CreateThumbnail(photo,image,thumbname,width,height)
puts "Create thumb of " + photo.path
thumb = image.first.resize_to_fill( width, height)

View File

@ -171,7 +171,7 @@ div.title {
width: 240px;
}
.row div.title img, .row div.image img {
width: 100px;
width: 100px;
height: 100px;
}
div.title img {